﻿using BP.CCBill;
using BP.DA;
using BP.Difference;
using BP.En;
using BP.Sys;
using BP.Port;
using BP.Sys.FrmUI;
using BP.Template.Printer;
using BP.WF.Data;
using BP.WF.Template.Frm;
using BP.WF.Template.SFlow;
using BP.WF.Template;
using System;
using System.Collections;
using System.Data;
using System.IO;
using BP.Web;
using NPOI.OpenXmlFormats.Dml;
using NLog.Web.LayoutRenderers;
using System.Runtime.CompilerServices;

namespace BP.WF
{
    /// <summary>
    /// 升级助手
    /// </summary>
    public class Upgrader
    {
        public static void InitDemo_Siji()
        {
            string sql = "SELECT No FROM En_CheLiangTaiZhang";
            DataTable dt = DBAccess.RunSQLReturnTable(sql);
            DBAccess.RunSQL("DELETE FROM En_CheLiangTaiZhangSiJi");

            foreach (DataRow dr in dt.Rows)
            {
                GEDtl gEEntity = new GEDtl("En_CheLiangTaiZhangSiJi");
                gEEntity.SetValByKey("RefPK", dr[0].ToString());

                gEEntity.OID = 0;
                gEEntity.SetValByKey("SiJiEmail", "guobaogeng@ccbpm.cn");
                gEEntity.SetValByKey("SiJiEmp", "guobaogeng");
                gEEntity.SetValByKey("SiJiName", "郭宝庚");
                gEEntity.SetValByKey("SiJiTel", "158888878898");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();

                gEEntity.OID = 0;
                gEEntity.SetValByKey("SiJiEmail", "zhoutianjiao@ccbpm.cn");
                gEEntity.SetValByKey("SiJiEmp", "zhoutianjiao");
                gEEntity.SetValByKey("SiJiName", "周天娇");
                gEEntity.SetValByKey("SiJiTel", "158888878898");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();

                gEEntity.OID = 0;
                gEEntity.SetValByKey("SiJiEmail", "liyan@ccbpm.cn");
                gEEntity.SetValByKey("SiJiEmp", "liyan");
                gEEntity.SetValByKey("SiJiName", "李岩");
                gEEntity.SetValByKey("SiJiTel", "158888878898");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();

            }

        }
        public static void InitDemo_Cheliang()
        {

            string sql = "SELECT No FROM En_CheLiangTaiZhang";
            DataTable mydt111 = DBAccess.RunSQLReturnTable(sql);

            DBAccess.RunSQL("DELETE FROM En_CheLiangTaiZhangCheLiangPeiJian");

            foreach (DataRow dr in mydt111.Rows)
            {
                GEDtl gEEntity = new GEDtl("En_CheLiangTaiZhangCheLiangPeiJian");
                gEEntity.SetValByKey("RefPK", dr[0].ToString());

                gEEntity.SetValByKey("DanJia", "500.00");
                gEEntity.SetValByKey("ShuLiang", "1");
                gEEntity.SetValByKey("XiaoJi", "500.00");
                gEEntity.SetValByKey("MingChen", "千斤顶");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();

                gEEntity.OID = 0;
                gEEntity.SetValByKey("DanJia", "100.00");
                gEEntity.SetValByKey("ShuLiang", "1");
                gEEntity.SetValByKey("XiaoJi", "100.00");
                gEEntity.SetValByKey("MingChen", "说明书");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();


                gEEntity.OID = 0;
                gEEntity.SetValByKey("DanJia", "4500.00");
                gEEntity.SetValByKey("ShuLiang", "1");
                gEEntity.SetValByKey("XiaoJi", "4500.00");
                gEEntity.SetValByKey("MingChen", "三角架");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();

                gEEntity.OID = 0;
                gEEntity.SetValByKey("DanJia", "4500.00");
                gEEntity.SetValByKey("ShuLiang", "1");
                gEEntity.SetValByKey("XiaoJi", "4500.00");
                gEEntity.SetValByKey("MingChen", "备胎");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();

                gEEntity.OID = 0;
                gEEntity.SetValByKey("DanJia", "100.00");
                gEEntity.SetValByKey("ShuLiang", "2");
                gEEntity.SetValByKey("XiaoJi", "200.00");
                gEEntity.SetValByKey("MingChen", "刹车片");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();

                gEEntity.OID = 0;
                gEEntity.SetValByKey("DanJia", "100.00");
                gEEntity.SetValByKey("ShuLiang", "2");
                gEEntity.SetValByKey("XiaoJi", "200.00");
                gEEntity.SetValByKey("MingChen", "进气套件");
                gEEntity.SetValByKey("RDT", DataType.CurrentDateTime);
                gEEntity.SetValByKey("Rec", WebUser.No);
                gEEntity.Insert();
            }
        }
        /// <summary>
        /// 当前版本号-为了升级使用.
        /// </summary>
        //public static int Ver = 20250217;
        public static int Ver = 20250520;
        /// <summary>
        /// 升级文件入口
        /// </summary>
        /// <returns></returns>
        public static string UpdataCCFlowVer()
        {

            if (Glo.CCBPMRunModel == CCBPMRunModel.SAAS)
                return "info@SAAS模式需要手工升级.";

            //BP.En.Method md123 = ClassFactory.GetMethod("BP.Demo.StockJudge");
            //md123.Do();

            //if (1 == 2 && SystemConfig.CustomerNo.Equals("KeLun") == true)
            //    UpdateMapExtH5();
            //InitDemo_Siji();
            //InitDemo_Cheliang();
            //初始化车辆.
            //  InitDemo_Cheliang();
            //InitDemo_Cheliang();

            #region 1.检查是否需要升级，并更新升级的业务逻辑.
            string updataNote = "";
            /*
             * 升级版本记录:
             * 20150330: 优化发起列表的效率, by:zhoupeng.
             * 2, 升级表单树,支持动态表单树.
             * 1, 执行一次Sender发送人的升级，原来由 GenerWorkerList 转入 WF_GenerWorkFlow.
             * 0, 静默升级启用日期.2014-12
             */
            if (DBAccess.IsExitsObject("Sys_Serial") == false)
                return "";
            //升级SQL
            UpdataCCFlowVerSQLScript();
            //判断数据库的版本.
            string sql = "SELECT IntVal FROM Sys_Serial WHERE CfgKey='Ver'";
            int currDBVer = DBAccess.RunSQLReturnValInt(sql, 0);
            if (currDBVer != null && currDBVer != 0 && currDBVer >= Ver)
                return null; // 不需要升级.

            WebUser.No = "admin";
            //初始化数据.
            InitDBSrc_Loacl();
            InitDBSrc_LocalWebApi();
            InitDBSrc_Weather();
            InitDBSrc_Stock();
            #endregion 检查是否需要升级，并更新升级的业务逻辑.


            #region 丢失从表GroupField.
            sql = "SELECT No,FK_MapData FROM Sys_MapDtl WHERE No NOT in(select CtrlID FROM Sys_GroupField)";
            DataTable d23t = DBAccess.RunSQLReturnTable(sql);
            foreach (DataRow dr in d23t.Rows)
            {
                string no = dr[0].ToString();
                MapDtl dtl = new MapDtl(no);
                GroupField mygf = new GroupField();
                mygf.Lab = dtl.Name;
                mygf.FrmID = dtl.FrmID;
                mygf.CtrlID = dtl.No;
                mygf.CtrlType = "Dtl";
                mygf.Insert();
            }
            #endregion 丢失从表.

            //用户组的升级
            Team team = new Team();
            if (DBAccess.IsExitsTableCol("Port_Team", "ParentNo") == false)
            {
                team.CheckPhysicsTable();
            }
            int num = team.Retrieve(TeamAttr.ParentNo, "0");
            if (num == 0)
            {
                TeamType teamType = new TeamType();
                teamType.SetValByKey(TeamAttr.Name, "类型1");
                teamType.SetValByKey(TeamAttr.Idx, "0");
                if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single)
                    teamType.SetValByKey(TeamAttr.OrgNo, WebUser.OrgNo);
                teamType.Insert();

                team.SetValByKey(TeamAttr.No, "001");
                team.SetValByKey(TeamAttr.Name, "用户组");
                team.SetValByKey(TeamAttr.ParentNo, "0");
                team.SetValByKey(TeamAttr.TeamTypeNo, teamType.No);
                if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single)
                    team.SetValByKey(TeamAttr.OrgNo, WebUser.OrgNo);
                team.SetValByKey(TeamAttr.Idx, 0);
                team.Insert();
            }

            DBAccess.RunSQL("UPDATE  Sys_MapAttr SET DefVal='@WebUser.DeptName' WHERE DefVal='@@WebUser.DeptName'");

            DBAccess.RunSQL("UPDATE GPM_PowerCenter SET CtrlModel='AdminerAndAdmin2' WHERE CtrlModel='AdminerAndAmin2'");
            DBAccess.RunSQL("UPDATE Sys_Enum SET Lab='经典表单' WHERE Lab='傻瓜表单'");
            if (DBAccess.IsExitsTableCol("WF_Node", "SelfFormEnRoot") == true)
                DBAccess.RunSQL("UPDATE WF_Node SET SelfFormEnRoot=0 WHERE SelfFormEnRoot IS NULL OR SelfFormEnRoot=''");

            //如果存在这个列.
            //   if (DBAccess.IsExitsTableCol("Sys_MapData", "BillCheckModel") == true)
            //     DBAccess.RunSQL("ALTER TABLE Sys_Mapdata MODIFY COLUMN BillCheckModel VARCHAR(20)");
            //创建索引.
            if (1 == 1 && 2 == 1)
            {
                BP.WF.DTS.CreateIndex idx = new DTS.CreateIndex();
                idx.Do();
                // BP.Port.Emp emp = new Emp();
                // emp.Insert();
                // emp.Update();
            }

            #region 升级后缀.
            DBAccess.RunSQL("UPDATE Sys_MapAttr SET DefVal='@WebUser.DeptName' WHERE DefVal='@WebUser.FK_DeptName'");
            DBAccess.RunSQL("UPDATE Sys_MapAttr SET UIWidth = 280 WHERE UIWidth = 100 AND KeyOfEn = 'Title'");
            sql = "SELECT MyPK FROM Sys_MapAttr WHERE AtPara NOT LIKE '%@suffix=%' AND MyDataType= 8";
            MapAttrs attrs = new MapAttrs();
            attrs.RetrieveInSQL(sql);
            foreach (MapAttr attr in attrs)
            {
                attr.SetPara("suffix", "￥");
                attr.Update();
            }
            DBAccess.RunSQL("UPDATE Sys_MapAttr SET DefVal='@WebUser.DeptNo'  WHERE DefVal='@WebUser.FK_Dept'");
            DBAccess.RunSQL("UPDATE Sys_MapAttr SET DefVal='@WebUser.DeptName'  WHERE DefVal='@WebUser.FK_DeptName'");
            #endregion 升级后缀

            #region 修复EntityType枚举值
            string isExitValue = "SELECT COUNT(MyPK) FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey='EntityType' " +
                                 "AND (IntKey=4 OR IntKey=5)";
            if (DBAccess.RunSQLReturnValInt(isExitValue) == 0)
            {
                //新增枚举4和5
                string insertEntityType = "INSERT INTO " + BP.Sys.Base.Glo.SysEnum() + " (MyPK, Lab, EnumKey, IntKey, " +
                                          "Lang, OrgNo, StrKey, RefPK, ValColor) VALUES ";
                DBAccess.RunSQL(insertEntityType + "('EntityType_CH_4', '调查问卷', 'EntityType', 4, 'CH', NULL, NULL, NULL, '#000')");
                DBAccess.RunSQL(insertEntityType + "('EntityType_CH_5', 'EntityNoName实体', 'EntityType', 5, 'CH', NULL, NULL, NULL, '#000')");
            }
            #endregion 修复EntityType枚举值

            #region 修复PGSQL字段默认值
            if (SystemConfig.AppCenterDBType == DBType.PostgreSQL)
            {
                try
                {
                    //获取要修复的表
                    ArrayList arrayList = ClassFactory.GetObjects("bp.en.Entity");
                    // 执行默认值更新
                    ExecutUpdateDefaultVal(arrayList);
                }
                catch (Exception e)
                {
                    if (e.Message != null && !e.Message.Contains("err@没有找到 TS."))
                    {
                        Log.DebugWriteError(e);
                    }
                }
            }
            #endregion 修复PGSQL字段默认值

            #region 升级从表的OID.
            MapDtls dtls = new MapDtls();
            dtls.RetrieveAll();
            foreach (MapDtl dtl in dtls)
            {
                sql = "SELECT IntVal FROM Sys_Serial WHERE CfgKey='" + dtl.PTable + "'";
                int val = DBAccess.RunSQLReturnValInt(sql, 0);
                if (val == 0)
                {
                    try
                    {
                        val = DBAccess.RunSQLReturnValInt("SELECT MAX(OID) AS A FROM " + dtl.PTable, 0);
                        DBAccess.RunSQL("INSERT INTO Sys_Serial (CfgKey,IntVal) VALUES ('" + dtl.PTable + "'," + val + ") ");
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            #endregion 升级从表的OID.

            #region 升级track表.
            if (SystemConfig.AppCenterDBType == DBType.MSSQL)
            {
                if (DBAccess.IsExitsObject("ModifyMyPK") == false)
                {
                    string sqlscript = BP.Difference.SystemConfig.CCFlowAppPath + "WF/Data/UpdateTrack_SQL.sql";
                    string str = DataType.ReadTextFile(sqlscript);
                    DBAccess.RunSQL(str);
                }
                DBAccess.RunSQL("EXEC ModifyMyPK");
            }
            else
            {
                DataTable dtFlows = DBAccess.RunSQLReturnTable("SELECT No FROM WF_Flow ");
                foreach (DataRow dr in dtFlows.Rows)
                {
                    string flowNo = dr[0].ToString();
                    string tableTrack = "ND" + int.Parse(flowNo) + "Track";
                    if (DBAccess.IsExitsObject(tableTrack) == false)
                        continue;

                    switch (SystemConfig.AppCenterDBType)
                    {
                        case DBType.MySQL:
                        case DBType.DM:
                            DBAccess.RunSQL("ALTER TABLE " + tableTrack + " MODIFY MyPK BIGINT");
                            break;
                        case DBType.MSSQL:
                            break;
                        case DBType.PostgreSQL:
                        case DBType.KingBaseR3:
                        case DBType.KingBaseR6:
                        case DBType.KingBaseR8:
                        case DBType.HGDB:
                            DBAccess.RunSQL("ALTER TABLE " + tableTrack + " ALTER COLUMN MyPK TYPE BIGINT");
                            break;
                        case DBType.Oracle:
                            string data_type = DBAccess.RunSQLReturnString("SELECT data_type FROM all_tab_columns WHERE table_name =  '" + tableTrack.ToUpper() + "' and column_name='MYPK'");
                            if (data_type != "NUMBER") {
                                DBAccess.RunSQL("ALTER TABLE " + tableTrack + " ADD MyPK NUMBER(19) NOT NULL");
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            #endregion 升级track表.

            #region 2.为科伦修复字段.
            FrmTrack frmTrack = new FrmTrack();
            frmTrack.CheckPhysicsTable();
            if (DBAccess.IsExitsTableCol("WF_PushMsg", "FlowNo") == false)
                DBAccess.RenameTableField("WF_PushMsg", "FK_Flow", "FlowNo");
            if (DBAccess.IsExitsTableCol("WF_GenerWorkerList", "NodeName") == false)
            {
                GenerWorkerList wl = new GenerWorkerList();
                wl.CheckPhysicsTable();
            }
            CCList listcc = new CCList();
            listcc.CheckPhysicsTable();
            #endregion 为科伦修复字段.

            #region 升级单据 .2024.09.
            //升级单据.
            GenerBill gb = new GenerBill();
            gb.CheckPhysicsTable();
            FrmBill fb = new FrmBill();
            fb.CheckPhysicsTable();
            GenerWorker gw = new GenerWorker();
            gw.CheckPhysicsTable();
            FrmBill bill = new FrmBill();
            bill.CheckPhysicsTable();
            #endregion 升级单据 .2024.09.

            #region 导入数据错误的问题.
            string sql1 = "SELECT No FROM Sys_MapData WHERE EnPK='No' AND No LIKE 'ND%'";
            DataTable dt23 = DBAccess.RunSQLReturnTable(sql1);
            foreach (DataRow dr in dt23.Rows)
            {
                string frmID = dr[0].ToString();
                MapData md = new MapData(frmID);
                md.EnPK = "OID";
                md.EntityType = EntityType.SingleFrm;
                md.Update();
                sql1 = "DELETE FROM Sys_MapAttr WHERE FK_Mapdata='" + md.No + "' AND KeyOfEn IN('No','Name','EntityState','RecNo','RecName','OrgNo','DeptNo')";
                DBAccess.RunSQL(sql1);
            }
            #endregion

            #region 单据，字典找不到目录.
            if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
            {
                DataTable dtFrms = DBAccess.RunSQLReturnTable("SELECT No,Name,FK_FormTree FROM Sys_MapData WHERE EntityType in (1,2,5)  AND FK_FormTree NOT IN (SELECT no from Sys_FormTree  )");
                string treeNo = DBAccess.RunSQLReturnString("SELECT NO FROM Sys_FormTree WHERE ParentNo='0'");
                foreach (DataRow dr in dtFrms.Rows)
                {
                    string no = dr[0].ToString();
                    DBAccess.RunSQL("update Sys_MapData set FK_FormTree='" + treeNo + "' where No='" + no + "' ");
                }
            }
            #endregion

            #region 升级FK_Dept 转换到 DeptNo.
            MapDatas md44s = new MapDatas();
            md44s.RetrieveIn("EntityType", "2,1"); //frmDict类型的实体.
            foreach (MapData md in md44s)
            {
                MapAttr attr = new MapAttr();
                attr.MyPK = md.No + "_FK_Dept";
                if (attr.RetrieveFromDBSources() == 1)
                {
                    attr.Delete(); //删除.
                    attr.KeyOfEn = "DeptNo";
                    attr.MyPK = md.No + "_" + attr.KeyOfEn;
                    try
                    {
                        attr.DirectInsert();
                    }
                    catch (Exception ex)
                    {
                    }
                    //处理数据.
                    if (DBAccess.IsExitsTableCol(md.PTable, "DeptNo") == false)
                    {
                        if (DBAccess.IsExitsTableCol(md.PTable, "FK_Dept") == true)
                            DBAccess.RenameTableField(md.PTable, "FK_Dept", "DeptNo");
                        else
                            DBAccess.CreateTableColumn(md.PTable, "DeptNo", DataType.AppString, "", "");
                    }
                }
                //  item.Turn2EntityNoName();
            }
            //处理升级失败，修复菜单问题.
            MapDatas mds = new MapDatas();
            mds.Retrieve("EntityType", 5); //frmDict类型的实体.
            foreach (MapData md in mds)
            {
                //修改菜单.
                string sq1l = "UPDATE GPM_Menu SET MenuModel='EntityNoName', AtPara='@EnName=TS.CCBill.FrmEntityNoName@EnPKVal=" + md.No + "', UrlPath ='/src/CCFast/CCBill/SearchEntityNoName.vue' WHERE FrmID='" + md.No + "'";
                DBAccess.RunSQL(sq1l);
                // md.Turn2EntityNoName();

                //删除描述字段.  
                DBAccess.RunSQL("DELETE FROM  Sys_MapAttr WHERE KeyOfEn IN ('Title','BillNo','OID','FID') AND FK_MapData='" + md.No + "'");

                //删除描述字段.
                DBAccess.RunSQL("DELETE FROM  Sys_MapAttr WHERE KeyOfEn IN ('BillState','Starter','StarterName','FK_Dept') AND FK_MapData='" + md.No + "'");
            }
            #endregion 升级FK_Dept 转换到 DeptNo.

            //执行sql.升级节点高度.
            if (DBAccess.IsExitsTableCol("WF_Node", "UIWidth") == true)
                DBAccess.RunSQL("UPDATE WF_Node SET UIWidth=120,UIHeight=60 WHERE UIWidth=0 OR UIWidth Is Null ");

            if (DBAccess.IsExitsTableCol("WF_Node", "NodeType") == true)
            {
                DBAccess.RunSQL("UPDATE WF_Node SET NodeType=0 WHERE NodeType IS NULL ");
                //UIContralType.Score
            }
            else
            {
                Node nd = new Node();
                nd.CheckPhysicsTable();
                DBAccess.RunSQL("UPDATE WF_Node SET NodeType=0 WHERE NodeType IS NULL ");
            }

            #region 升级枚举值 - 兼容H5版本.
            DBAccess.RunSQL("UPDATE Sys_EnumMain SET AtPara ='@EnName=TS.FrmUI.SysEnumMainInt' WHERE AtPara IS NULL OR AtPara =''");
            DBAccess.RunSQL("UPDATE Sys_EnumMain SET EnumKey =No WHERE EnumKey IS NULL OR EnumKey =''");

            SysEnumMains mains = new SysEnumMains();
            mains.RetrieveInSQL("No", "SELECT no FROM Sys_EnumMain WHERE Val0 = '' OR Val0 IS NULL ");
            foreach (SysEnumMain en in mains)
            {
                en.DoInitDtls();
            }
            #endregion 升级枚举值 - 兼容H5版本.

            #region Sys_MapExt的pop弹窗的升级
            MapExts mapExts = new MapExts();
            mapExts.RetrieveIn(MapExtAttr.ExtModel, "'Pop','MultipleChoiceSearch'");
            foreach (MapExt mapExt in mapExts)
            {
                string mypk = mapExt.FrmID + "_" + mapExt.AttrOfOper;
                MapAttr mapAttr = new MapAttr();
                mapAttr.MyPK = mypk + "T";
                if (mapAttr.RetrieveFromDBSources() == 0)
                {
                    mapAttr.MyPK = mypk;
                    mapAttr.RetrieveFromDBSources();
                    mapAttr.MyPK = mapAttr.MyPK + 'T';
                    mapAttr.setKeyOfEn(mapAttr.KeyOfEn + 'T');
                    mapAttr.UIVisible = false;
                    mapAttr.setUIIsEnable(false);
                    mapAttr.Insert();
                }
            }
            #endregion Sys_MapExt的pop弹窗的升级

            #region 重命名字段. 2023.11.30
            if (DBAccess.IsExitsTableCol("WF_CH", "FK_Flow") == true)
            {
                CH chen = new CH();
                chen.CheckPhysicsTable();
            }
            //WF_PushMsg..
            if (DBAccess.IsExitsTableCol("WF_PushMsg", "FK_Flow") == true)
            {
                PushMsg pm1 = new PushMsg();
                pm1.CheckPhysicsTable();
                //DBAccess.RenameTableField("WF_PushMsg", "FK_Event", "EventNo");
                //DBAccess.RenameTableField("WF_PushMsg", "FK_Node", "NodeID");
                //DBAccess.RenameTableField("WF_PushMsg", "FK_Flow", "FlowNo");

                DBAccess.RunSQL("UPDATE WF_PushMsg SET FlowNo=FK_Flow WHERE FlowNo IS  NULL ");
                if (DBAccess.IsExitsTableCol("WF_PushMsg", "FK_Node") == true)
                    DBAccess.RunSQL("UPDATE WF_PushMsg SET NodeID=FK_Node WHERE FlowNo IS  NULL");
                if (DBAccess.IsExitsTableCol("WF_PushMsg", "FK_Event") == true)
                    DBAccess.RunSQL("UPDATE WF_PushMsg SET EventNo=FK_Event WHERE FlowNo IS  NULL ");

                DBAccess.DropTableColumn("WF_PushMsg", "FK_Flow");
                DBAccess.DropTableColumn("WF_PushMsg", "FK_Node");
                DBAccess.DropTableColumn("WF_PushMsg", "FK_Event");
            }

            if (DBAccess.IsExitsTableCol("Sys_FrmEvent", "FK_Flow") == true)
            {
                FrmEvent myfe = new FrmEvent();
                myfe.CheckPhysicsTable();

                DBAccess.RunSQL("UPDATE Sys_FrmEvent SET FlowNo=FK_Flow WHERE FlowNo IS NULL ");
                DBAccess.RunSQL("UPDATE Sys_FrmEvent SET NodeID=FK_Node WHERE NodeID IS NULL OR NodeID=0 ");

                DBAccess.DropTableColumn("Sys_FrmEvent", "FK_Flow");
                DBAccess.DropTableColumn("Sys_FrmEvent", "FK_Node");
            }
            #endregion 重命名字段.

            #region 2023.07.02 升级字典表,查询.
            SFSearch search = new SFSearch();
            search.CheckPhysicsTable();
            Sys.SFTable table = new Sys.SFTable();
            table.CheckPhysicsTable();
            SFProc enProduce = new SFProc();
            enProduce.CheckPhysicsTable();
            #endregion 2023.07.02 

            #region 升级SFTable中SrcType为DBSrcType 
            if (DBAccess.IsExitsTableCol("Sys_SFTable", "SrcType") == true)
            {
                if (DBAccess.IsExitsTableCol("Sys_SFTable", "DBSrcType") == false)
                {
                    switch (SystemConfig.AppCenterDBType)
                    {
                        case DBType.MSSQL:
                            DBAccess.RunSQL("ALTER TABLE Sys_SFTable ADD DBSrcType NVARCHAR(20) DEFAULT 'BPClass' NULL");
                            break;
                        case DBType.Oracle:
                        case DBType.Informix:
                        case DBType.PostgreSQL:
                        case DBType.HGDB:
                        case DBType.UX:
                        case DBType.KingBaseR3:
                        case DBType.KingBaseR6:
                        case DBType.DM:
                        case DBType.GBASE8CByOracle:
                            DBAccess.RunSQL("ALTER TABLE Sys_SFTable ADD DBSrcType VARCHAR(20) DEFAULT 'BPClass' NULL");
                            break;
                        case DBType.MySQL:
                            DBAccess.RunSQL("ALTER TABLE Sys_SFTable ADD DBSrcType NVARCHAR(20) DEFAULT 'BPClass' NULL");
                            break;
                        default:
                            break;
                    }

                }
                DBAccess.RunSQL("UPDATE Sys_SFTable SET DBSrcType=(CASE SrcType WHEN  0 THEN 'BPClass' WHEN 1 THEN 'CreateTable' WHEN 1 THEN 'CreateTable' " +
                    "WHEN 2 THEN 'TableOrView' WHEN 3 THEN 'SQL' WHEN 4 THEN 'WebServices' WHEN 5 THEN 'Handler' WHEN 6 THEN 'JQuery' " +
                    "WHEN 7 THEN 'SysDict' ELSE 'WebApi' END)");
                DBAccess.DropTableColumn("Sys_SFTable", "SrcType");

            }
            #endregion 升级SFTable中SrcType为DBSrcType

            #region 升级流程模式的存储方式
            if (DBAccess.IsExitsTableCol("WF_Flow", "FlowDevModel") == false)
            {
                switch (SystemConfig.AppCenterDBType)
                {
                    case DBType.MSSQL:
                        DBAccess.RunSQL("ALTER TABLE WF_Flow ADD FlowDevModel INT NULL");
                        break;
                    case DBType.Oracle:
                    case DBType.Informix:
                    case DBType.PostgreSQL:
                    case DBType.HGDB:
                    case DBType.UX:
                    case DBType.KingBaseR3:
                    case DBType.KingBaseR6:
                    case DBType.DM:
                    case DBType.GBASE8CByOracle:
                        DBAccess.RunSQL("ALTER TABLE WF_Flow ADD FlowDevModel INTEGER NULL");
                        break;
                    case DBType.MySQL:
                        DBAccess.RunSQL("ALTER TABLE WF_Flow ADD FlowDevModel INT NULL");
                        break;
                    default:
                        break;
                }
            }
            Flows flows = new Flows();
            QueryObject qo = new QueryObject(flows);
            qo.AddWhere("AtPara", "Like", "%FlowDevModel=%");
            qo.DoQuery();
            foreach (Flow flow in flows)
            {
                int flowDevModel = flow.GetParaInt("FlowDevModel");
                flow.FlowDevModel = (FlowDevModel)flowDevModel;
                string atPara = flow.GetValStringByKey("AtPara");
                atPara = atPara.Replace("@FlowDevModel=" + flowDevModel, "");
                flow.SetValByKey("AtPara", atPara);
                flow.DirectUpdate();
            }
            #endregion 升级流程模式的存储方式

            #region 升级文本框字段类型.  TextModel=0普通文本，1密码，2=TextArea,3=富文本.
            //说明没有升级. TextModel=0
            if (DBAccess.IsExitsTableCol("Sys_MapAttr", "IsRichText") == true)
            {
                MapAttr ma = new MapAttr();
                ma.CheckPhysicsTable();

                sql = "UPDATE Sys_MapAttr SET TextModel=3 WHERE IsRichText=1 OR AtPara LIKE '%IsRichText=1%'";
                DBAccess.RunSQL(sql);

                sql = "UPDATE Sys_MapAttr SET TextModel=2 WHERE UIHeight >=40 OR IsSupperText=1";
                DBAccess.RunSQL(sql);

                //sql = "UPDATE Sys_MapAttr SET TextModel=1 WHERE IsSecret=1";
                //DBAccess.RunSQL(sql);

                DBAccess.DropTableColumn("Sys_MapAttr", "IsRichText");
                DBAccess.DropTableColumn("Sys_MapAttr", "IsSecret");
            }
            #endregion 升级文本框字段类型

            #region 统一升级主键. 给多对多的实体增加主键.
            if (DBAccess.IsExitsTableCol("WF_NodeStation", "MyPK") == false)
            {
                //1.首先要删除主键.
                DBAccess.DropTablePK("WF_NodeStation");
                if (BP.Difference.SystemConfig.AppCenterDBType.ToString() == "MySQL")
                {
                    DBAccess.RunSQL("ALTER TABLE WF_NodeStation ADD COLUMN MyPK VARCHAR(100)");
                    DBAccess.RunSQL("UPDATE WF_NodeStation SET MyPK= CONCAT(FK_Node,'_',FK_Station)");
                }
                else
                    if (BP.Difference.SystemConfig.AppCenterDBType.ToString() == "MSSQL")
                {
                    DBAccess.RunSQL("ALTER TABLE WF_NodeStation ADD  MyPK VARCHAR(100)");
                    DBAccess.RunSQL("UPDATE WF_NodeStation SET MyPK= CONVERT(varchar,FK_Node)+'_'+FK_Station");
                }

                //2. 自动创建.
                NodeStation ns = new NodeStation();
                ns.CheckPhysicsTable();
            }

            if (DBAccess.IsExitsObject("WF_NodeTeam") == false)
            {
                NodeTeam nt = new NodeTeam();
                nt.CheckPhysicsTable();
            }

            if (DBAccess.IsExitsTableCol("WF_NodeTeam", "MyPK") == false)
            {
                DBAccess.DropTablePK("WF_NodeTeam");
                if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
                {
                    DBAccess.RunSQL("ALTER TABLE WF_NodeTeam ADD COLUMN MyPK VARCHAR(100)");
                    DBAccess.RunSQL("UPDATE WF_NodeTeam SET MyPK= CONCAT(FK_Node,'_',FK_Team)");
                }
                else if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MSSQL)
                {
                    DBAccess.RunSQL("ALTER TABLE WF_NodeTeam ADD  MyPK VARCHAR(100)");
                    DBAccess.RunSQL("UPDATE WF_NodeTeam SET MyPK= CONVERT(varchar,FK_Node)+'_'+FK_Team");
                }
                //3. 执行更新.
            }

            if (DBAccess.IsExitsTableCol("WF_NodeEmp", "MyPK") == false)
            {

                DBAccess.DropTablePK("WF_NodeEmp");
                if (BP.Difference.SystemConfig.AppCenterDBType.ToString() == "MySQL")
                {
                    DBAccess.RunSQL("ALTER TABLE WF_NodeEmp ADD COLUMN MyPK VARCHAR(100)");
                    DBAccess.RunSQL("UPDATE WF_NodeEmp SET MyPK= CONCAT(FK_Node,'_',FK_Emp)");
                }
                else
           if (BP.Difference.SystemConfig.AppCenterDBType.ToString() == "MSSQL")
                {

                    DBAccess.RunSQL("ALTER TABLE WF_NodeEmp ADD  MyPK VARCHAR(100)");
                    DBAccess.RunSQL("UPDATE WF_NodeEmp SET MyPK= CONVERT(varchar,FK_Node)+'_'+FK_Emp");
                }
                NodeEmp ne1 = new NodeEmp();
                ne1.CheckPhysicsTable();
                //3. 执行更新.

            }
            #endregion 统一升级主键.

            #region 系统更新.
            //升级支持ts.
            // UpdataTSModel();
            //升级日志.
            UserLogLevel ul = new UserLogLevel();
            ul.CheckPhysicsTable();
            UserLogType ut = new UserLogType();
            ut.CheckPhysicsTable();

            //添加IsEnable
            //BP.WF.Template.FlowTab fb = new BP.WF.Template.FlowTab();
            //fb.CheckPhysicsTable();

            if (DBAccess.IsExitsTableCol("Sys_GroupField", "EnName") == true)
            {
                GroupField groupField = new GroupField();
                groupField.CheckPhysicsTable();
                DBAccess.RunSQL("UPDATE Sys_GroupField SET FrmID=enName WHERE FrmID IS null");
            }

            //升级.
            Auth ath = new Auth();
            ath.CheckPhysicsTable();

            //检查BPM.现在暂时不使用原菜单结构
            // if (!SystemConfig.OrganizationIsView)
            //    CheckGPM();

            MapData mapData = new MapData();
            mapData.CheckPhysicsTable();

            #endregion 系统更新.

            #region 升级优化集团版的应用. 2020.04.03
            //--2020.05.28 升级方向条件;
            BP.WF.Template.Cond cond = new Cond();
            cond.CheckPhysicsTable();
            if (DBAccess.IsExitsTableCol("WF_Cond", "PRI") == true)
            {
                DBAccess.RunSQL("UPDATE WF_Cond SET Idx=PRI ");
                DBAccess.DropTableColumn("WF_Cond", "PRI");
            }

            //修改节点类型,合并属性.
            /*if (DBAccess.IsExitsTableCol("WF_Node", "SubThreadType") == true)
            {
                DBAccess.RunSQLReturnTable("UPDATE WF_Node SET RunModel=5 WHERE SubThreadType=1 ");
                DBAccess.DropTableColumn("WF_Node", "SubThreadType");
            }*/

            //--2020.05.25 修改节点自定义按钮功能;
            BP.WF.Template.NodeToolbar bar = new NodeToolbar();
            bar.CheckPhysicsTable();
            if (DBAccess.IsExitsTableCol("WF_NodeToolbar", "ShowWhere") == true)
            {
                DBAccess.RunSQL("UPDATE WF_NodeToolbar SET IsMyFlow = 1 Where ShowWhere = 1");
                DBAccess.RunSQL("UPDATE WF_NodeToolbar SET IsMyCC = 1 Where ShowWhere = 2");

                DBAccess.DropTableColumn("WF_NodeToolbar", "ShowWhere");
            }
            switch (BP.Difference.SystemConfig.AppCenterDBType)
            {
                case DBType.Oracle:
                case DBType.GBASE8CByOracle:
                    DBAccess.RunSQL("UPDATE Sys_MapAttr set tag=''");
                    break;
                default:
                    break;
            }
            Direction direction = new Direction();
            direction.CheckPhysicsTable();

            MapAttr myattr = new MapAttr();
            myattr.CheckPhysicsTable();

            MapDtlExt mde = new MapDtlExt();
            mde.CheckPhysicsTable();

            NodeExt ne = new NodeExt();
            ne.CheckPhysicsTable();

            FlowExt fe = new FlowExt();
            fe.CheckPhysicsTable();

            //检查frmTrack.
            BP.CCBill.Track tk = new BP.CCBill.Track();
            tk.CheckPhysicsTable();

            BP.Port.DeptEmpStation des = new BP.Port.DeptEmpStation();
            des.CheckPhysicsTable();

            BP.Port.DeptEmp de = new BP.Port.DeptEmp();
            de.CheckPhysicsTable();

            BP.Port.Emp emp1 = new BP.Port.Emp();
            emp1.CheckPhysicsTable();

            BP.WF.Port.Admin2Group.Org org = new BP.WF.Port.Admin2Group.Org();
            org.CheckPhysicsTable();

            BP.WF.Template.FlowSort fs = new BP.WF.Template.FlowSort();
            fs.CheckPhysicsTable();

            FlowOrg fo = new FlowOrg();
            fo.CheckPhysicsTable();

            BP.Sys.SysEnumMain sem = new SysEnumMain();
            sem.CheckPhysicsTable();

            BP.Sys.SysEnum myse = new SysEnum();
            myse.CheckPhysicsTable();

            //检查表.
            BP.Sys.GloVar gv = new GloVar();
            gv.CheckPhysicsTable();

            //检查表.
            BP.Sys.EnCfg cfg = new BP.Sys.EnCfg();
            cfg.CheckPhysicsTable();

            //检查表.
            SysFormTree frmTree = new SysFormTree();
            frmTree.CheckPhysicsTable();

            BP.Sys.SFTable sf = new BP.Sys.SFTable();
            sf.CheckPhysicsTable();

            FrmSubFlow sb = new FrmSubFlow();
            sb.CheckPhysicsTable();

            BP.WF.Template.PushMsg pm = new PushMsg();
            pm.CheckPhysicsTable();

            //修复数据表.
            BP.Sys.GroupField gf = new GroupField();
            gf.CheckPhysicsTable();

            #endregion 升级优化集团版的应用

            #region 升级子流程.
            //检查子流程表.
            if (DBAccess.IsExitsObject("WF_NodeSubFlow") == true)
            {
                if (DBAccess.IsExitsTableCol("WF_NodeSubFlow", "OID") == true)
                {
                    DBAccess.RunSQL("DROP TABLE WF_NodeSubFlow");
                    SubFlowHand sub = new SubFlowHand();
                    sub.CheckPhysicsTable();
                }
            }
            #endregion 升级子流程.

            #region 升级方向条件. 2020.06.02
            if (DBAccess.IsExitsTableCol("WF_Cond", "CondOrAnd") == true)
            {
                DataTable dt = DBAccess.RunSQLReturnTable("SELECT DISTINCT FK_Node, toNodeID, CondOrAnd, CondType  FROM wf_cond WHERE DataFrom!=100 ");
                foreach (DataRow dr in dt.Rows)
                {
                    int nodeID = int.Parse(dr["FK_Node"].ToString());
                    int toNodeID = int.Parse(dr["toNodeID"].ToString());

                    Conds conds = new Conds();
                    conds.Retrieve(CondAttr.FK_Node, nodeID,
                        CondAttr.ToNodeID, toNodeID, CondAttr.Idx);

                    //判断是否需要修复？
                    if (conds.Count == 1 || conds.Count == 0)
                        continue;

                    //判断是否有？
                    bool isHave = false;
                    foreach (Cond myCond in conds)
                    {
                        if (myCond.HisDataFrom == ConnDataFrom.CondOperator)
                            isHave = true;
                    }
                    if (isHave == true)
                        continue;

                    //获得类型.
                    int OrAndType = DBAccess.RunSQLReturnValInt("SELECT  CondOrAnd  FROM wf_cond WHERE FK_Node=" + nodeID, -1);
                    if (OrAndType == -1)
                        continue;

                    int idx = 0;
                    int idxSave = 0;
                    int count = conds.Count;
                    foreach (Cond item in conds)
                    {
                        idx++;

                        idxSave++;
                        item.Idx = idxSave;
                        item.Update();

                        if (count == idx)
                            continue;

                        Cond operCond = new Cond();
                        operCond.Copy(item);
                        operCond.setMyPK(DBAccess.GenerGUID());
                        operCond.HisDataFrom = ConnDataFrom.CondOperator;

                        if (OrAndType == 0)
                        {
                            operCond.OperatorValue = " OR ";
                            operCond.Note = " OR ";
                            operCond.OperatorValue = " OR ";
                            operCond.Note = " OR ";
                        }
                        else
                        {
                            operCond.OperatorValue = " AND ";
                            operCond.Note = " AND ";
                            operCond.OperatorValue = " AND ";
                            operCond.Note = " AND ";
                        }

                        idxSave++;
                        operCond.Idx = idxSave;
                        operCond.Insert();
                    }
                }

                //升级后删除指定的列.
                DBAccess.DropTableColumn("WF_Cond", "CondOrAnd");
                DBAccess.DropTableColumn("WF_Cond", "NodeID");
            }
            #endregion 升级方向条件.

            #region 重新生成view WF_EmpWorks,  2013-08-06.
           //BP.WF.Install.ReCreateView();
            #endregion


            #region 升级视图. 解决厦门信息港的 - 流程监控与授权.
            //if (DBAccess.IsExitsObject("V_MyFlowData") == false)
            //{
            //    BP.WF.Template.PowerModel pm11 = new PowerModel();
            //    pm11.CheckPhysicsTable();

            //    sql = "CREATE VIEW V_MyFlowData ";
            //    sql += " AS ";
            //    sql += " SELECT A.*, B.EmpNo as MyEmpNo FROM WF_GenerWorkflow A, WF_PowerModel B ";
            //    sql += " WHERE  A.FK_Flow=B.FlowNo AND B.PowerCtrlType=1 AND A.WFState>= 2";
            //    sql += "    UNION  ";
            //    sql += " SELECT A.*, c.No as MyEmpNo FROM WF_GenerWorkflow A, WF_PowerModel B, Port_Emp C, Port_DeptEmpStation D";
            //    sql += " WHERE  A.FK_Flow=B.FlowNo  AND B.PowerCtrlType=0 AND C.No=D.FK_Emp AND B.StaNo=D.FK_Station AND A.WFState>=2";
            //    DBAccess.RunSQL(sql);
            //}
            #endregion 升级视图.

            //升级从表的 fk_node .
            //获取需要修改的从表
            string dtlNos = DBAccess.RunSQLReturnString("SELECT B.NO  FROM SYS_GROUPFIELD A, SYS_MAPDTL B WHERE A.CTRLTYPE='Dtl' AND A.CTRLID=B.NO AND FK_NODE!=0");
            if (DataType.IsNullOrEmpty(dtlNos) == false)
            {
                dtlNos = dtlNos.Replace(",", "','");
                dtlNos = "('" + dtlNos + "')";
                DBAccess.RunSQL("UPDATE SYS_MAPDTL SET FK_NODE=0 WHERE NO IN " + dtlNos);
            }
            FrmNode nff = new FrmNode();
            nff.CheckPhysicsTable();

            #region 更新节点名称.
            switch (SystemConfig.AppCenterDBType)
            {
                case DBType.MSSQL:
                case DBType.PostgreSQL:
                case DBType.HGDB:
                case DBType.KingBaseR3:
                case DBType.KingBaseR6:
                    sql = " UPDATE WF_Direction SET ToNodeName = WF_Node.Name FROM WF_Node ";
                    sql += " WHERE WF_Direction.ToNode = WF_Node.NodeID ";
                    break;
                case DBType.Oracle:
                case DBType.DM:
                case DBType.GBASE8CByOracle:
                    sql = "UPDATE WF_Direction E SET ToNodeName=(SELECT U.Name FROM WF_Node U WHERE E.ToNode=U.NodeID) WHERE EXISTS (SELECT 1 FROM WF_Node U WHERE E.ToNode=U.NodeID)";
                    break;
                default:
                    sql = "UPDATE WF_Direction A, WF_Node B SET A.ToNodeName=B.Name WHERE A.ToNode=B.NodeID ";
                    break;
            }
            DBAccess.RunSQL(sql);

            //更新groupField.
            switch (BP.Difference.SystemConfig.AppCenterDBType)
            {
                case DBType.MySQL:
                    sql = "UPDATE Sys_MapDtl, Sys_GroupField B SET Sys_MapDtl.GroupField=B.OID WHERE Sys_MapDtl.No=B.CtrlID AND Sys_MapDtl.GroupField=''";
                    break;
                case DBType.Oracle:
                case DBType.DM:
                case DBType.GBASE8CByOracle:
                    sql = "UPDATE Sys_MapDtl E SET GroupField=(SELECT U.OID FROM Sys_GroupField U WHERE E.No=U.CtrlID) WHERE EXISTS (SELECT 1 FROM Sys_GroupField U WHERE E.No=U.CtrlID AND E.GroupField='')";
                    DBAccess.RunSQL("UPDATE Sys_MapAttr set tag=''");
                    break;
                case DBType.MSSQL:
                default:
                    sql = "UPDATE Sys_MapDtl SET GroupField=Sys_GroupField.OID FROM Sys_GroupField WHERE Sys_MapDtl.No=Sys_GroupField.CtrlID AND Sys_MapDtl.GroupField=''";
                    break;
            }
            DBAccess.RunSQL(sql);
            #endregion 更新节点名称.

            #region 升级审核组件
            if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
                sql = "UPDATE WF_FrmNode F INNER JOIN(SELECT FWCSta,NodeID FROM WF_Node ) N on F.FK_Node = N.NodeID and  F.IsEnableFWC =1 SET F.IsEnableFWC = N.FWCSta;";
            if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MSSQL)
                sql = "UPDATE    F SET IsEnableFWC = N. FWCSta  FROM WF_FrmNode F,WF_Node N    WHERE F.FK_Node = N.NodeID AND F.IsEnableFWC =1";
            if (BP.Difference.SystemConfig.AppCenterDBType == DBType.Oracle || DBAccess.AppCenterDBType == DBType.GBASE8CByOracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR3 || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR6 || DBAccess.AppCenterDBType == DBType.DM)
                sql = "UPDATE WF_FrmNode F  SET (IsEnableFWC)=(SELECT FWCSta FROM WF_Node N WHERE F.FK_Node = N.NodeID AND F.IsEnableFWC =1)";
            if (BP.Difference.SystemConfig.AppCenterDBType == DBType.PostgreSQL || BP.Difference.SystemConfig.AppCenterDBType == DBType.HGDB || BP.Difference.SystemConfig.AppCenterDBType == DBType.HGDB || BP.Difference.SystemConfig.AppCenterDBType == DBType.UX)
                sql = "UPDATE WF_FrmNode SET IsEnableFWC=(SELECT FWCSta FROM WF_Node N Where N.NodeID=WF_FrmNode.FK_Node AND WF_FrmNode.IsEnableFWC=1)";

            DBAccess.RunSQL(sql);
            #endregion 升级审核组件

            #region 升级填充数据.
            /*//pop自动填充
            MapExts exts1 = new MapExts();
            qo = new QueryObject(exts1);
            qo.AddWhere(MapExtAttr.ExtType, " LIKE ", "Pop%");
            qo.DoQuery();
            foreach (MapExt ext in exts1)
            {
                string mypk = ext.FrmID + "_" + ext.AttrOfOper;
                MapAttr ma = new MapAttr();
                ma.setMyPK(mypk);
                if (ma.RetrieveFromDBSources() == 0)
                {
                    ext.Delete();
                    continue;
                }

                if (ma.GetParaString("PopModel") == ext.ExtType)
                    continue; //已经修复了，或者配置了.

                ma.SetPara("PopModel", ext.ExtType);
                ma.Update();

                if (DataType.IsNullOrEmpty(ext.Tag4) == true)
                    continue;

                MapExt extP = new MapExt();
                extP.setMyPK(ext.MyPK + "_FullData");
                int i = extP.RetrieveFromDBSources();
                if (i == 1)
                    continue;

                extP.ExtType = "FullData";
                extP.FrmID = ext.FrmID;
                extP.AttrOfOper = ext.AttrOfOper;
                extP.DBType = ext.DBType;
                extP.Doc = ext.Tag4;
                extP.Insert(); //执行插入.
            }


            //文本自动填充
            MapExts exts = new MapExts();
            exts.Retrieve(MapExtAttr.ExtType, MapExtXmlList.TBFullCtrl);
            foreach (MapExt ext in exts)
            {
                string mypk = ext.FrmID + "_" + ext.AttrOfOper;
                MapAttr ma = new MapAttr();
                ma.setMyPK(mypk);
                if (ma.RetrieveFromDBSources() == 0)
                {
                    ext.Delete();
                    continue;
                }
                string modal = ma.GetParaString("TBFullCtrl");
                if (DataType.IsNullOrEmpty(modal) == false)
                    continue; //已经修复了，或者配置了.

                if (DataType.IsNullOrEmpty(ext.Tag3) == false)
                    ma.SetPara("TBFullCtrl", "Simple");
                else
                    ma.SetPara("TBFullCtrl", "Table");

                ma.Update();

                if (DataType.IsNullOrEmpty(ext.Tag4) == true)
                    continue;

                MapExt extP = new MapExt();
                extP.setMyPK(ext.MyPK + "_FullData");
                int i = extP.RetrieveFromDBSources();
                if (i == 1)
                    continue;

                extP.ExtType = "FullData";
                extP.FrmID = ext.FrmID;
                extP.AttrOfOper = ext.AttrOfOper;
                extP.DBType = ext.DBType;
                extP.Doc = ext.Tag4;

                //填充从表
                extP.Tag1 = ext.Tag1;
                //填充下拉框
                extP.Tag = ext.Tag;

                extP.Insert(); //执行插入.
            }

            //下拉框填充其他控件
            //文本自动填充
            exts = new MapExts();
            exts.Retrieve(MapExtAttr.ExtType, MapExtXmlList.DDLFullCtrl);
            foreach (MapExt ext in exts)
            {
                string mypk = ext.FrmID + "_" + ext.AttrOfOper;
                MapAttr ma = new MapAttr();
                ma.setMyPK(mypk);
                if (ma.RetrieveFromDBSources() == 0)
                {
                    ext.Delete();
                    continue;
                }
                string modal = ma.GetParaString("IsFullData");
                if (DataType.IsNullOrEmpty(modal) == false)
                    continue; //已经修复了，或者配置了.

                //启用填充其他控件
                ma.SetPara("IsFullData", 1);
                ma.Update();


                MapExt extP = new MapExt();
                extP.setMyPK(ext.MyPK + "_FullData");
                int i = extP.RetrieveFromDBSources();
                if (i == 1)
                    continue;

                extP.ExtType = "FullData";
                extP.FrmID = ext.FrmID;
                extP.AttrOfOper = ext.AttrOfOper;
                extP.DBType = ext.DBType;
                extP.Doc = ext.Doc;


                //填充从表
                extP.Tag1 = ext.Tag1;
                //填充下拉框
                extP.Tag = ext.Tag;

                extP.Insert(); //执行插入.

            }

            //装载填充
            exts = new MapExts();
            exts.Retrieve(MapExtAttr.ExtType, MapExtXmlList.PageLoadFull);
            foreach (MapExt ext in exts)
            {
                mapData.No = ext.FrmID;
                if (mapData.RetrieveFromDBSources() == 0)
                {
                    ext.Delete();
                    continue;
                }
                if (DataType.IsNullOrEmpty(mapData.GetParaString("IsPageLoadFull")) == false)
                    continue;
                mapData.ItIsPageLoadFull = true;
                mapData.Update();

                //修改填充数据的值
                ext.Doc = ext.Tag;

                string tag1 = ext.Tag1;
                if (DataType.IsNullOrEmpty(tag1) == true)
                {
                    ext.Update();
                    continue;
                }
                MapDtls dtls = mapData.MapDtls;
                foreach (MapDtl dtl in dtls)
                {
                    tag1 = tag1.Replace("*" + dtl.No + "=", "$" + dtl.No + ":");
                }
                ext.Tag1 = tag1;
                ext.Update();
            }*/
            #endregion 升级 填充数据.

            string msg = "";
            try
            {

                #region 升级事件.
                if (DBAccess.IsExitsTableCol("Sys_FrmEvent", "DoType") == true)
                {
                    BP.Sys.FrmEvent frmevent = new FrmEvent();
                    frmevent.CheckPhysicsTable();

                    DBAccess.RunSQL("UPDATE Sys_FrmEvent SET EventDoType=DoType  ");
                    DBAccess.RunSQL("ALTER TABLE Sys_FrmEvent  DROP COLUMN	DoType  ");
                }
                #endregion

                #region 修复丢失的发起人.
                Flows fls = new Flows();
                fls.GetNewEntity.CheckPhysicsTable();

                foreach (Flow item in fls)
                {
                    if (DBAccess.IsExitsObject(item.PTable) == false)
                        continue;
                    try
                    {
                        DBAccess.RunSQL(" UPDATE " + item.PTable + " SET FlowStarter =(SELECT Starter FROM WF_GENERWORKFLOW WHERE " + item.PTable + ".Oid=WF_GENERWORKFLOW.WORKID)");
                    }
                    catch (Exception ex)
                    {
                        //   GERpt rpt=new GERpt(
                    }
                }
                #endregion 修复丢失的发起人.

                #region 给city 设置拼音.
                if (DBAccess.IsExitsObject("Demo_City") == true && 1 == 2)
                {
                    if (DBAccess.IsExitsTableCol("Demo_City", "PinYin") == true)
                    {
                        /*为Demo_City 设置拼音.*/
                        sql = "SELECT No,Names FROM Demo_City ";
                        DataTable dtCity = DBAccess.RunSQLReturnTable(sql);

                        foreach (DataRow dr in dtCity.Rows)
                        {
                            string no = dr["No"].ToString();
                            string name = dr["Names"].ToString();
                            string pinyin1 = DataType.ParseStringToPinyin(name);
                            string pinyin2 = DataType.ParseStringToPinyinJianXie(name);
                            string pinyin = "," + pinyin1 + "," + pinyin2 + ",";
                            DBAccess.RunSQL("UPDATE Demo_City SET PinYin='" + pinyin + "' WHERE NO='" + no + "'");
                        }
                    }
                }
                #endregion 给city 设置拼音.

                //增加列FlowStars
                BP.WF.Port.WFEmp wfemp = new BP.WF.Port.WFEmp();
                wfemp.CheckPhysicsTable();

                DBType dbtype = BP.Difference.SystemConfig.AppCenterDBType;

                BP.Sys.FrmRB rb = new FrmRB();
                rb.CheckPhysicsTable();


                MapDtlExt dtlExt = new MapDtlExt();
                dtlExt.CheckPhysicsTable();

                //删除枚举.
                DBAccess.RunSQL("DELETE FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey IN ('SelectorModel','CtrlWayAth')");

                //2017.5.19 打印模板字段修复
                FrmPrintTemplate bt = new FrmPrintTemplate();
                bt.CheckPhysicsTable();
                if (DBAccess.IsExitsTableCol("Sys_FrmPrintTemplate", "url") == true)
                    DBAccess.RunSQL("UPDATE Sys_FrmPrintTemplate SET TempFilePath = Url WHERE TempFilePath IS null");

                //规范升级根目录.
                DataTable dttree = DBAccess.RunSQLReturnTable("SELECT No FROM Sys_FormTree WHERE ParentNo='-1' ");
                if (dttree.Rows.Count == 1)
                {
                    DBAccess.RunSQL("UPDATE Sys_FormTree SET ParentNo='1' WHERE ParentNo='0' ");
                    DBAccess.RunSQL("UPDATE Sys_FormTree SET No='1' WHERE No='0'  ");
                    DBAccess.RunSQL("UPDATE Sys_FormTree SET ParentNo='0' WHERE No='1'");
                }

                //删除垃圾数据.
                BP.Sys.MapExt.DeleteDB();

                //升级经典表单.
                MapFrmFool mff = new MapFrmFool();
                mff.CheckPhysicsTable();

                #region 表单方案中的不可编辑, 旧版本如果包含了这个列.
                if (DBAccess.IsExitsTableCol("WF_FrmNode", "IsEdit") == true)
                {
                    /*如果存在这个列,就查询出来=0的设置，就让其设置为不可以编辑的。*/
                    sql = "UPDATE WF_FrmNode SET FrmSln=1 WHERE IsEdit=0 ";
                    DBAccess.RunSQL(sql);

                    sql = "UPDATE WF_FrmNode SET IsEdit=100000";
                    DBAccess.RunSQL(sql);
                }
                #endregion

                //执行升级 2016.04.08 
                Cond cnd = new Cond();
                cnd.CheckPhysicsTable();

                #region  增加week字段,方便按周统计.
                sql = "SELECT WorkID,RDT FROM WF_GenerWorkFlow WHERE WeekNum=0 or WeekNum is null ";
                DataTable dt = DBAccess.RunSQLReturnTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    sql = "UPDATE WF_GenerWorkFlow SET WeekNum=" + DataType.CurrentWeekGetWeekByDay(dr[1].ToString().Replace("+", " ")) + " WHERE WorkID=" + dr[0].ToString();
                    DBAccess.RunSQL(sql);
                }

                //查询.
                BP.WF.Data.CH ch = new CH();
                ch.CheckPhysicsTable();

                sql = "SELECT MyPK,DTFrom FROM WF_CH WHERE WeekNum=0 or WeekNum is null ";
                dt = DBAccess.RunSQLReturnTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    sql = "UPDATE WF_CH SET WeekNum=" + DataType.CurrentWeekGetWeekByDay(dr[1].ToString()) + " WHERE MyPK='" + dr[0].ToString() + "'";
                    DBAccess.RunSQL(sql);
                }
                #endregion  增加week字段.

                #region 检查数据源.
                SFDBSrc src = new SFDBSrc();
                src.No = "local";
                src.Name = "本机数据源";
                if (src.RetrieveFromDBSources() == 0)
                {
                    src.SetPara("EnName", "TS.Sys.SFDBSrcSQL");
                    src.Insert();
                }

                src = new SFDBSrc();
                src.No = "localWebApi";
                src.Name = "本机WebApi数据源";
                if (src.RetrieveFromDBSources() == 0)
                {
                    src.DBSrcType = "WebApi";
                    src.DBName = "本机WebApi";
                    src.SetValByKey("ConnString", "http://localhost:56146/WF/Demo/");
                    src.SetPara("EnName", "TS.Sys.SFDBSrcWebApi");
                    src.Insert();
                }
                #endregion 检查数据源.

                #region 20170613.增加审核组件配置项“是否显示退回的审核信息”对应字段 by:liuxianchen
                try
                {
                    if (DBAccess.IsExitsTableCol("WF_Node", "FWCIsShowReturnMsg") == false)
                    {
                        switch (src.HisDBType)
                        {
                            case DBType.MSSQL:
                                DBAccess.RunSQL("ALTER TABLE WF_Node ADD FWCIsShowReturnMsg INT NULL");
                                break;
                            case DBType.Oracle:
                            case DBType.Informix:
                            case DBType.PostgreSQL:
                            case DBType.HGDB:
                            case DBType.UX:
                            case DBType.KingBaseR3:
                            case DBType.KingBaseR6:
                            case DBType.DM:
                            case DBType.GBASE8CByOracle:
                                DBAccess.RunSQL("ALTER TABLE WF_Node ADD FWCIsShowReturnMsg INTEGER NULL");
                                break;
                            case DBType.MySQL:
                                DBAccess.RunSQL("ALTER TABLE WF_Node ADD FWCIsShowReturnMsg INT NULL");
                                break;
                            default:
                                break;
                        }

                        DBAccess.RunSQL("UPDATE WF_Node SET FWCIsShowReturnMsg = 0");
                    }
                }
                catch
                {
                }
                #endregion

                #region 20170522.增加SL表单设计器中对单选/复选按钮进行字体大小调节的功能 by:liuxianchen

                FrmRB frmRB = new FrmRB();
                frmRB.CheckPhysicsTable();

                try
                {
                    DataTable columns = src.GetColumns("Sys_FrmRB");
                    if (columns.Select("No='AtPara'").Length == 0)
                    {
                        switch (src.HisDBType)
                        {
                            case DBType.MSSQL:
                                DBAccess.RunSQL("ALTER TABLE Sys_FrmRB ADD AtPara NVARCHAR(1000) NULL");
                                break;
                            case DBType.Oracle:
                            case DBType.DM:
                            case DBType.KingBaseR3:
                            case DBType.KingBaseR6:
                            case DBType.GBASE8CByOracle:
                                DBAccess.RunSQL("ALTER TABLE Sys_FrmRB ADD AtPara NVARCHAR2(1000) NULL");
                                break;
                            case DBType.PostgreSQL:
                            case DBType.UX:
                            case DBType.HGDB:
                                DBAccess.RunSQL("ALTER TABLE Sys_FrmRB ADD AtPara VARCHAR2(1000) NULL");
                                break;
                            case DBType.MySQL:
                            case DBType.Informix:
                                DBAccess.RunSQL("ALTER TABLE Sys_FrmRB ADD AtPara TEXT NULL");
                                break;
                            default:
                                break;
                        }
                    }
                }
                catch
                {
                }
                #endregion

                #region 其他.
                // 更新 PassRate.
                sql = "UPDATE WF_Node SET PassRate=100 WHERE PassRate IS NULL";
                DBAccess.RunSQL(sql);
                #endregion 其他.

                #region 升级统一规则.
                #region 检查必要的升级。
                NodeWorkCheck fwc = new NodeWorkCheck();
                fwc.CheckPhysicsTable();

                Flow myfl = new Flow();
                myfl.CheckPhysicsTable();

                Node nd = new Node();
                nd.CheckPhysicsTable();

                //Sys_SFDBSrc
                SFDBSrc sfDBSrc = new SFDBSrc();
                sfDBSrc.CheckPhysicsTable();
                #endregion 检查必要的升级。
                MapExt mapExt = new MapExt();
                mapExt.CheckPhysicsTable();

                /* try
                 {
                     string sqls = "";

                     if (BP.Difference.SystemConfig.AppCenterDBType == DBType.Oracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.HGDB || BP.Difference.SystemConfig.AppCenterDBType == DBType.PostgreSQL || BP.Difference.SystemConfig.AppCenterDBType == DBType.UX || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR3 || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR6 || DBAccess.AppCenterDBType == DBType.DM || DBAccess.AppCenterDBType == DBType.GBASE8CByOracle)
                     {
                         sqls += "UPDATE Sys_MapExt SET MyPK= ExtType||'_'||FK_Mapdata||'_'||AttrOfOper WHERE ExtType='" + MapExtXmlList.TBFullCtrl + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK= ExtType||'_'||FK_Mapdata||'_'||AttrOfOper WHERE ExtType='" + MapExtXmlList.PopVal + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK= ExtType||'_'||FK_Mapdata||'_'||AttrOfOper WHERE ExtType='" + MapExtXmlList.DDLFullCtrl + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK= ExtType||'_'||FK_Mapdata||'_'||AttrsOfActive WHERE ExtType='" + MapExtXmlList.ActiveDDL + "'";
                     }
                     else if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
                     {
                         sqls += "UPDATE Sys_MapExt SET MyPK=CONCAT(ExtType,'_',FK_Mapdata,'_',AttrOfOper) WHERE ExtType='" + MapExtXmlList.TBFullCtrl + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK=CONCAT(ExtType,'_',FK_Mapdata,'_',AttrOfOper) WHERE ExtType='" + MapExtXmlList.PopVal + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK=CONCAT(ExtType,'_',FK_Mapdata,'_',AttrOfOper) WHERE ExtType='" + MapExtXmlList.DDLFullCtrl + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK=CONCAT(ExtType,'_',FK_Mapdata,'_',AttrOfOper) WHERE ExtType='" + MapExtXmlList.ActiveDDL + "'";
                     }
                     else
                     {
                         sqls += "UPDATE Sys_MapExt SET MyPK= ExtType+'_'+FK_Mapdata+'_'+AttrOfOper WHERE ExtType='" + MapExtXmlList.TBFullCtrl + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK= ExtType+'_'+FK_Mapdata+'_'+AttrOfOper WHERE ExtType='" + MapExtXmlList.PopVal + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK= ExtType+'_'+FK_Mapdata+'_'+AttrOfOper WHERE ExtType='" + MapExtXmlList.DDLFullCtrl + "'";
                         sqls += "@UPDATE Sys_MapExt SET MyPK= ExtType+'_'+FK_Mapdata+'_'+AttrsOfActive WHERE ExtType='" + MapExtXmlList.ActiveDDL + "'";

                     }

                     DBAccess.RunSQLs(sqls);
                 }
                 catch (Exception ex)
                 {
                     BP.DA.Log.DebugWriteError(ex.Message);
                 }*/
                #endregion

                #region 其他.
                //升级表单树. 2015.10.05
                SysFormTree sft = new SysFormTree();
                sft.CheckPhysicsTable();


                //表单信息表.
                MapDataExt mapext = new MapDataExt();
                mapext.CheckPhysicsTable();

                TransferCustom tc = new TransferCustom();
                tc.CheckPhysicsTable();

                //增加部门字段。
                CCList cc = new CCList();
                cc.CheckPhysicsTable();
                #endregion 其他.

                #region 升级sys_sftable
                //升级
                BP.Sys.SFTable tab = new BP.Sys.SFTable();
                tab.CheckPhysicsTable();
                #endregion

                #region 基础数据更新.
                Node wf_Node = new Node();
                wf_Node.CheckPhysicsTable();
                // 设置节点ICON.
                sql = "UPDATE WF_Node SET ICON='审核.png' WHERE ICON IS NULL";
                DBAccess.RunSQL(sql);

                BP.WF.Template.NodeSheet nodeSheet = new BP.WF.Template.NodeSheet();
                nodeSheet.CheckPhysicsTable();

                #endregion 基础数据更新.

                #region 升级SelectAccper

                SelectAccper selectAccper = new SelectAccper();
                selectAccper.CheckPhysicsTable();
                #endregion

                #region  升级 NodeToolbar
                FrmField ff = new FrmField();
                ff.CheckPhysicsTable();

                SysFormTree ssft = new SysFormTree();
                ssft.CheckPhysicsTable();

                BP.Sys.FrmAttachment myath = new FrmAttachment();
                myath.CheckPhysicsTable();

                FrmField ffs = new FrmField();
                ffs.CheckPhysicsTable();
                #endregion

                #region 执行sql．

                //删除垃圾数据.
                DBAccess.RunSQL("DELETE FROM WF_FrmNode WHERE  MyPK  LIKE '%\\_\\_%'");
                DBAccess.RunSQL("DELETE  FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey in ('PrintFileType','EventDoType','FormType','BatchRole','StartGuideWay','NodeFormType')");
                DBAccess.RunSQL("UPDATE Sys_FrmSln SET FK_Flow =(SELECT FK_FLOW FROM WF_Node WHERE NODEID=Sys_FrmSln.FK_Node) WHERE FK_Flow IS NULL");

                if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MSSQL)
                    DBAccess.RunSQL("UPDATE WF_FrmNode SET MyPK=FK_Frm+'_'+convert(varchar,FK_Node )+'_'+FK_Flow");

                if (BP.Difference.SystemConfig.AppCenterDBType == DBType.Oracle || DBAccess.AppCenterDBType == DBType.GBASE8CByOracle || BP.Difference.SystemConfig.AppCenterDBType == DBType.HGDB || BP.Difference.SystemConfig.AppCenterDBType == DBType.PostgreSQL || BP.Difference.SystemConfig.AppCenterDBType == DBType.UX || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR3 || BP.Difference.SystemConfig.AppCenterDBType == DBType.KingBaseR6 || DBAccess.AppCenterDBType == DBType.DM)
                    DBAccess.RunSQL("UPDATE WF_FrmNode SET MyPK=FK_Frm||'_'||FK_Node||'_'||FK_Flow");

                if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
                {
                    //   DBAccess.RunSQL("UPDATE WF_FrmNode SET MyPK=CONCAT(FK_Frm,'_',FK_Node,'_',FK_Flow)");
                }

                #endregion

                #region 执行更新.wf_node
                sql = "UPDATE WF_Node SET FWCType=0 WHERE FWCType IS NULL";
                sql += "@UPDATE WF_Node SET FWC_H=0 WHERE FWC_H IS NULL";
                DBAccess.RunSQLs(sql);

                sql = "UPDATE WF_Node SET SFSta=0 WHERE SFSta IS NULL";
                sql += "@UPDATE WF_Node SET SF_H=0 WHERE SF_H IS NULL";
                DBAccess.RunSQLs(sql);
                #endregion 执行更新.

                #region 升级站内消息表 2013-10-20
                BP.WF.SMS sms = new SMS();
                sms.CheckPhysicsTable();
                #endregion


                #region 升级Img
                FrmImg img = new FrmImg();
                img.CheckPhysicsTable();

                ExtImg myimg = new ExtImg();
                myimg.CheckPhysicsTable();
                if (DBAccess.IsExitsTableCol("Sys_FrmImg", "SrcType") == true)
                {
                    DBAccess.RunSQL("UPDATE Sys_FrmImg SET ImgSrcType = SrcType WHERE ImgSrcType IS NULL");
                    DBAccess.RunSQL("UPDATE Sys_FrmImg SET ImgSrcType = 0 WHERE ImgSrcType IS NULL");
                }
                #endregion

                #region 修复 mapattr UIHeight, UIWidth 类型错误.
                switch (BP.Difference.SystemConfig.AppCenterDBType)
                {
                    case DBType.Oracle:
                    case DBType.DM:
                    case DBType.KingBaseR3:
                    case DBType.KingBaseR6:
                    case DBType.GBASE8CByOracle:
                        msg = "@Sys_MapAttr 修改字段";
                        break;
                    case DBType.MSSQL:
                        msg = "@修改sql server控件高度和宽度字段。";
                        DBAccess.RunSQL("ALTER TABLE Sys_MapAttr ALTER COLUMN UIWidth float");
                        DBAccess.RunSQL("ALTER TABLE Sys_MapAttr ALTER COLUMN UIHeight float");
                        break;
                    default:
                        break;
                }
                #endregion

                #region 升级常用词汇
                switch (BP.Difference.SystemConfig.AppCenterDBType)
                {
                    case DBType.Oracle:
                    case DBType.DM:
                    case DBType.KingBaseR3:
                    case DBType.KingBaseR6:
                    case DBType.GBASE8CByOracle:
                        int i = DBAccess.RunSQLReturnCOUNT("SELECT * FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'SYS_DEFVAL' AND COLUMN_NAME = 'PARENTNO'");
                        if (i == 0)
                        {
                            if (DBAccess.IsExitsObject("SYS_DEFVAL") == true)
                                DBAccess.RunSQL("drop table Sys_DefVal");

                            DefVal dv = new DefVal();
                            dv.CheckPhysicsTable();
                        }
                        msg = "@Sys_DefVal 修改字段";
                        break;
                    case DBType.MSSQL:
                        msg = "@修改 Sys_DefVal。";
                        i = DBAccess.RunSQLReturnCOUNT("SELECT * FROM SYSCOLUMNS WHERE ID=OBJECT_ID('Sys_DefVal') AND NAME='ParentNo'");
                        if (i == 0)
                        {
                            if (DBAccess.IsExitsObject("Sys_DefVal") == true)
                                DBAccess.RunSQL("drop table Sys_DefVal");

                            DefVal dv = new DefVal();
                            dv.CheckPhysicsTable();
                        }
                        break;
                    default:
                        break;
                }
                #endregion

                #region 登录更新错误
                msg = "@登录时错误。";
                DBAccess.RunSQL("DELETE FROM " + BP.Sys.Base.Glo.SysEnum() + " WHERE EnumKey IN ('DeliveryWay','RunModel','OutTimeDeal','FlowAppType')");
                #endregion 登录更新错误

                #region 执行admin登录. 2012-12-25 新版本.
                Emp emp = new Emp("admin");
                if (emp.RetrieveFromDBSources() == 1)
                {
                    BP.Web.WebUser.SignInOfGener(emp);
                }
                else
                {
                    emp.SetValByKey("No", "admin");
                    emp.Name = "admin";
                    emp.DeptNo = "01";
                    emp.Pass = "123";
                    emp.Insert();
                    BP.Web.WebUser.SignInOfGener(emp);
                    //throw new Exception("admin 用户丢失，请注意大小写。");
                }
                #endregion 执行admin登录.

                #region 修复 Sys_FrmImg 表字段 ImgAppType Tag0
                switch (BP.Difference.SystemConfig.AppCenterDBType)
                {
                    case DBType.Oracle:
                    case DBType.DM:
                    case DBType.KingBaseR3:
                    case DBType.KingBaseR6:
                    case DBType.GBASE8CByOracle:
                        int i = DBAccess.RunSQLReturnCOUNT("SELECT * FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'SYS_FRMIMG' AND COLUMN_NAME = 'TAG0'");
                        if (i == 0)
                        {
                            DBAccess.RunSQL("ALTER TABLE SYS_FRMIMG ADD (ImgAppType number,TAG0 nvarchar(500))");
                        }
                        msg = "@Sys_FrmImg 修改字段";
                        break;
                    case DBType.MSSQL:
                        msg = "@修改 Sys_FrmImg。";
                        i = DBAccess.RunSQLReturnCOUNT("SELECT * FROM SYSCOLUMNS WHERE ID=OBJECT_ID('Sys_FrmImg') AND NAME='Tag0'");
                        if (i == 0)
                        {
                            DBAccess.RunSQL("ALTER TABLE Sys_FrmImg ADD ImgAppType int");
                            DBAccess.RunSQL("ALTER TABLE Sys_FrmImg ADD Tag0 nvarchar(500)");
                        }
                        break;
                    default:
                        break;
                }
                #endregion

                #region 20161101.升级表单，增加图片附件必填验证 by:liuxianchen

                BP.Sys.FrmImgAth imgAth = new BP.Sys.FrmImgAth();
                imgAth.CheckPhysicsTable();

                sql = "UPDATE Sys_FrmImgAth SET IsRequired = 0 WHERE IsRequired IS NULL";
                DBAccess.RunSQL(sql);
                #endregion

                #region 20170520.附件删除规则修复
                try
                {
                    DataTable columns = src.GetColumns("Sys_FrmAttachment");
                    if (columns.Select("No='DeleteWay'").Length > 0 && columns.Select("No='IsDelete'").Length > 0)
                    {
                        DBAccess.RunSQL("UPDATE SYS_FRMATTACHMENT SET DeleteWay=IsDelete WHERE DeleteWay IS NULL");
                    }
                }
                catch
                {
                }
                #endregion

                #region 密码加密
                if (BP.Difference.SystemConfig.isEnablePasswordEncryption == true && DBAccess.IsView("Port_Emp", BP.Difference.SystemConfig.AppCenterDBType) == false)
                {
                    BP.Port.Emps emps = new BP.Port.Emps();
                    emps.RetrieveAllFromDBSource();
                    Object type = SystemConfig.GetIsPasswordEncryptionType();
                    String key = SystemConfig.GetIsEncryptionKey();
                    foreach (Emp empEn in emps)
                    {
                        if (string.IsNullOrEmpty(empEn.Pass) || empEn.Pass.Length < 30)
                        {
                            //BCrypt加密类型
                            if (type != null && type.ToString().Equals("2"))
                            {
                                empEn.Pass = BP.Tools.Cryptography.HashPassword(empEn.Pass);
                            }
                            //MD5加密类型
                            if (type != null && type.ToString().Equals("1"))
                            {
                                empEn.Pass = BP.Tools.Cryptography.MD5_Encrypt(empEn.Pass);
                            }
                            empEn.DirectUpdate();
                        }
                    }
                }
                #endregion

                // 最后更新版本号，然后返回.
                sql = "UPDATE Sys_Serial SET IntVal=" + Ver + " WHERE CfgKey='Ver'";
                if (DBAccess.RunSQL(sql) == 0)
                {
                    sql = "INSERT INTO Sys_Serial (CfgKey,IntVal) VALUES('Ver'," + Ver + ") ";
                    DBAccess.RunSQL(sql);
                }
                // 返回版本号.
                return "旧版本:(" + currDBVer + ")新版本:(" + Ver + ")"; // +"\t\n解决问题:" + updataNote;
            }
            catch (Exception ex)
            {
                string err = "问题出处:" + ex.Message + "<hr>" + msg + "<br>详细信息:@" + ex.StackTrace + "<br>@<a href='../DBInstall.htm' >点这里到系统升级界面。</a>";
                BP.DA.Log.DebugWriteError("系统升级错误:" + err);
                return err;
            }
        }

        private static void ExecutUpdateDefaultVal(ArrayList arrayList)
        {
            foreach (Entity en in arrayList)
            {
                if (DataType.IsNullOrEmpty(en.ToString()))
                    continue;
                Attrs attrs = en.EnMap.Attrs;
                foreach (Attr attr in attrs)
                {
                    switch (attr.MyDataType)
                    {
                        case DataType.AppInt:
                        case DataType.AppBoolean:
                        case DataType.AppFloat:
                        case DataType.AppMoney:
                        case DataType.AppDouble:
                            if (attr.DefaultVal != null)
                            {
                                if (DBAccess.IsExitsTableCol(en.EnMap.PhysicsTable, attr.Field))
                                {
                                    DBAccess.RunSQL("ALTER TABLE " + en.EnMap.PhysicsTable + " ALTER COLUMN " + attr.Field + " SET DEFAULT " + attr.DefaultVal);
                                }
                                else
                                {
                                    String dataType = "";
                                    if (attr.MyDataType == DataType.AppInt || attr.MyDataType == DataType.AppBoolean)
                                    {
                                        dataType = "INT";
                                    }
                                    else
                                    {
                                        dataType = "FLOAT";
                                    }
                                    if (en.IsExits == false)
                                        continue;

                                    DBAccess.RunSQL("ALTER TABLE " + en.EnMap.PhysicsTable + " ADD COLUMN " + attr.Field + " " + dataType + " DEFAULT " + attr.DefaultVal);
                                    Log.DebugWriteInfo("修复了表[" + en.EnMap.PhysicsTable + "]字段[" + attr.Field + "]的默认值，修改为[" + attr.DefaultVal + "]");
                                }
                            }
                            break;
                    }
                }
            }
        }
        /// <summary>
        /// 本机数据源
        /// </summary>
        public static void InitDBSrc_Loacl()
        {
            #region 数据源
            SFDBSrc dbSrc = new SFDBSrc();
            dbSrc.No = "local";
            dbSrc.Name = "本机数据源";
            if (dbSrc.RetrieveFromDBSources() == 0)
            {
                dbSrc.SetValByKey("DBSrcType", "local"); //设置类型. 
                dbSrc.SetValByKey("ConnString", "");
                dbSrc.SetValByKey("WebApiResultModel", "0");
                dbSrc.Insert();
            }
            #endregion
            BP.Sys.SFTable table = new BP.Sys.SFTable();

            #region 字典 - 无参数的字典.
            int idx = 0;
            table.No = "PianQu";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("片区");
                table.SelectStatement = "SELECT No,Name FROM Demo_PQ ";
                table.SFDBSrcNo = "local";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            table.No = "ShengFen";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("全部省份");
                table.SelectStatement = "SELECT No,Name FROM Demo_ShengFen";
                table.SFDBSrcNo = "local";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            table.No = "City";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("全部城市");
                table.SelectStatement = "SELECT No,Name FROM Demo_City";
                table.SFDBSrcNo = "local";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "MyDeptEmps";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("我部门的同事");
                table.SelectStatement = "SELECT No,Name FROM Port_Emp WHERE FK_Dept='@WebUser.DeptNo'";
                table.SetValByKey("SFDBSrcNo", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            table.No = "MyDepts";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("我的部门");
                table.SelectStatement = "SELECT A.No,A.Name FROM Port_Dept A, Port_DeptEmp B WHERE A.No=B.FK_Dept AND B.FK_Emp='@WebUser.No' ";
                table.SetValByKey("SFDBSrcNo", "local");

                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.SetValByKey("RootNo", "@WebUser.DeptNo");
                table.CodeStruct = CodeStruct.NoName;
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            table.No = "MyStations";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("我的岗位");
                table.SelectStatement = "SELECT A.No,A.Name FROM Port_Station A, Port_DeptEmpStation B WHERE A.No=B.FK_Station AND B.FK_Emp='@WebUser.No' ";
                table.SetValByKey("SFDBSrcNo", "local");
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.CodeStruct = CodeStruct.NoName;
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "Depts";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("部门");
                table.SelectStatement = "SELECT No,Name,ParentNo FROM Port_Dept ";
                table.SetValByKey("SFDBSrcNo", "local");
                table.CodeStruct = CodeStruct.Tree;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLTree");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.SetValByKey("RootNo", "100"); //演示环境.
                table.SetValByKey("IsPara", "0");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "MySubDepts";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("我的子部门树");
                table.SelectStatement = "SELECT No,Name,ParentNo FROM Port_Dept WHERE ParentNo='@WebUser.DeptNo' OR No='@WebUser.DeptNo' ";
                table.SetValByKey("SFDBSrcNo", "local");
                table.CodeStruct = CodeStruct.Tree;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLTree");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.SetValByKey("RootNo", "@WebUser.DeptNo");
                table.SetValByKey("IsPara", "0");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            #endregion 字典 - 无参数的字典.

            #region 字典 - 有参数的字典.
            table.No = "DeptEmps";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("人员");
                table.SelectStatement = "SELECT No,Name FROM Port_Emp WHERE FK_Dept='@Key'";
                table.SFDBSrcNo = "local";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.SetValByKey("IsPara", "1");
                table.SetValByKey("TestParas", " @Key=100");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "SubDepts";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("子部门");
                table.SelectStatement = "SELECT No,Name,ParentNo FROM Port_Dept WHERE ParentNo='@Key'";
                table.SFDBSrcNo = "local";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.SetValByKey("IsPara", "1");
                table.SetValByKey("TestParas", " @Key=100");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "PianQuPara";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("省份(根据片区)");
                table.SelectStatement = "SELECT No,Name FROM Demo_ShengFen WHERE FK_PQ='@Key'";
                table.SFDBSrcNo = "local";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.SetValByKey("IsPara", "1");
                table.SetValByKey("TestParas", "@Key=DB");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "CityPara";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.setName("城市(根据省份)");
                table.SelectStatement = "SELECT No,Name FROM Demo_City WHERE ShengFen='@Key'";
                table.SFDBSrcNo = "local";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                table.SetValByKey("FK_SFDBSrc", "local");
                table.SetValByKey("DBSrcType", "SQL");
                table.SetValByKey("IsPara", "1");
                table.SetValByKey("TestParas", "@Key=50");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            #endregion 字典 - 有参数的字典.

            BP.Sys.SFSearch search = new BP.Sys.SFSearch();

            #region 查询 
            search.No = "MyDeptStations";
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("我的部门岗位");
                search.SetValByKey("SelectStatement", "SELECT A.FK_Dept as DeptNo, B.Name as DeptName, A.FK_Station AS StationNo, C.Name as StationName FROM Port_DeptEmpStation A, Port_Dept B, Port_Station C WHERE A.FK_Dept=B.No AND A.FK_Station=C.No AND A.FK_Emp='@WebUser.No'");
                search.SetValByKey("ResultNum", 1); //实体类型. 
                search.SetPara("EnName", "TS.FrmUI.SFSearchSQL");
                search.SetValByKey("FK_SFDBSrc", "local");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }

            search.No = "Emp";
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("人员实体");
                search.SetValByKey("SelectStatement", "SELECT No as EmpNo,Name as EmpName,Tel,Email FROM Port_Emp WHERE No='@Key'");
                search.SetValByKey("ResultNum", 0); //实体类型. 
                search.SetPara("EnName", "TS.FrmUI.SFSearchSQL");
                search.SetValByKey("FK_SFDBSrc", "local");
                search.SetValByKey("TestParas", "@Key=yuwen");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            search.No = "Emps";
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("人员集合");
                search.SetValByKey("SelectStatement", "SELECT No as EmpNo,Name as EmpName,Tel,Email FROM Port_Emp WHERE FK_Dept='@Key'");
                search.SetValByKey("ResultNum", 1); //集合类型. 
                search.SetPara("EnName", "TS.FrmUI.SFSearchSQL");
                search.SetValByKey("FK_SFDBSrc", "local");
                search.SetValByKey("TestParas", "@Key=1001");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }

            search.No = "Student";
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("学生实体");
                search.SetValByKey("SelectStatement", "SELECT No as StudentNo,Name as StudentName,Tel,Email,Age,FK_BanJi FROM Demo_Student WHERE No='@Key'");
                search.SetValByKey("ResultNum", 0); //实体类型.
                search.SetPara("EnName", "TS.FrmUI.SFSearchSQL");
                search.SetValByKey("FK_SFDBSrc", "local");
                search.SetValByKey("TestParas", "@Key=0001");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            search.No = "Students";
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("学生集合");
                search.SetValByKey("SelectStatement", "SELECT No as StudentNo,Name as StudentName,Tel,Email,Age,FK_BanJi FROM Demo_Student WHERE FK_BanJi='@Key'");
                search.SetValByKey("ResultNum", 1); //集合类型. 
                search.SetPara("EnName", "TS.FrmUI.SFSearchSQL");
                search.SetValByKey("FK_SFDBSrc", "local");
                search.SetValByKey("TestParas", "@Key=001");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 查询.

            SFProc porc = new SFProc();

            #region 过程 -sql 
            porc.No = "ChenagePass";
            if (porc.RetrieveFromDBSources() == 0)
            {
                porc.setName("修改密码");
                porc.SetValByKey("SelectStatement", "UPDATE Port_Emp SET Pas11s='@Password' WHERE No='@Key' AND 1=2");
                porc.SetPara("EnName", "TS.FrmUI.SFProcSQL");
                porc.SetValByKey("FK_SFDBSrc", "local");
                idx = idx++;
                porc.SetValByKey("Idx", idx);
                porc.Insert();
            }
            porc.No = "DisEmp";
            if (porc.RetrieveFromDBSources() == 0)
            {
                porc.setName("禁用人员登录");
                porc.SetValByKey("SelectStatement", "UPDATE Port_Emp SET EmpSta=1 WHERE No='@Key' ");
                porc.SetPara("EnName", "TS.FrmUI.SFProcSQL");
                porc.SetValByKey("FK_SFDBSrc", "local");
                idx = idx++;
                porc.SetValByKey("Idx", idx);
                porc.Insert();
            }
            porc.No = "UnDisEmp";
            if (porc.RetrieveFromDBSources() == 0)
            {
                porc.setName("启用人员登录");
                porc.SetValByKey("SelectStatement", "UPDATE Port_Emp SET EmpSta=0 WHERE No='@Key' ");
                porc.SetPara("EnName", "TS.FrmUI.SFProcSQL");
                porc.SetValByKey("FK_SFDBSrc", "local");
                idx = idx++;
                porc.SetValByKey("Idx", idx);
                porc.Insert();
            }

            porc.No = "KickOutUserOnline";
            if (porc.RetrieveFromDBSources() == 0)
            {
                porc.setName("踢出人员登录");
                porc.SetValByKey("SelectStatement", "DELETE FROM Port_Token WHERE EmpNo='@Key' ");
                porc.SetPara("EnName", "TS.FrmUI.SFProcSQL");
                porc.SetValByKey("FK_SFDBSrc", "local");
                idx = idx++;
                porc.SetValByKey("Idx", idx);
                porc.Insert();
            }
            porc.No = "DeleteMsgByWorkID";
            if (porc.RetrieveFromDBSources() == 0)
            {
                porc.setName("删除流程消息提示");
                porc.SetValByKey("SelectStatement", "DELETE FROM Sys_SMS WHERE WorkID='@Key' ");
                porc.SetPara("EnName", "TS.FrmUI.SFProcSQL");
                porc.SetValByKey("FK_SFDBSrc", "local");
                idx = idx++;
                porc.SetValByKey("Idx", idx);
                porc.Insert();
            }
            #endregion 过程 

            #region 枚举
            SysEnumMain sems = new SysEnumMain();
            sems.No = "XB";
            if (sems.RetrieveFromDBSources() == 0)
            {
                sems.CfgVal = "@0=女@1=男";
                sems.Insert(); //插入.
                sems.DoInitDtls();
            }

            sems = new SysEnumMain();
            sems.No = "ZZMM";
            if (sems.RetrieveFromDBSources() == 0)
            {
                sems.CfgVal = "@0=群中@1=团员@2=党员@3=无党派人士";
                sems.Insert(); //插入.
                sems.DoInitDtls();
            }
            #endregion 枚举

        }
        /// <summary>
        /// 本机webapi数据源
        /// </summary>
        public static void InitDBSrc_LocalWebApi()
        {
            #region 数据源
            SFDBSrc dbSrc = new SFDBSrc();
            dbSrc.No = "localWebApi";
            dbSrc.Name = "本机WebApi";
            if (dbSrc.RetrieveFromDBSources() == 0)
            {
                //dbSrc.DBSrcType = "WebApi";
                dbSrc.SetValByKey("DBSrcType", "WebApi"); //设置类型. 
                dbSrc.SetValByKey("ConnString", SystemConfig.HostURL);
                dbSrc.SetPara("EnName", "TS.Sys.SFDBSrcWebApi");
                dbSrc.SetValByKey("TokenGetWay", "None");
                dbSrc.SetValByKey("WebApiResultModel", "0");
                dbSrc.Insert();
            }
            //检查环境变量.
            SFApiParas paras = new SFApiParas();
            paras.Retrieve("AttrKey", "Token", "DBSrcNo", dbSrc.No);
            if (paras.Count == 0)
            {
                SFApiPara para = new SFApiPara();
                para.SetValByKey("AttrKey", "Token");
                para.SetValByKey("DBSrcNo", "localWebApi");
                para.SetValByKey("AttrName", "(loalWebApi数据源)获取Token");
                para.SetValByKey("ApiParaModel", "1");
                para.SetValByKey("ApiParaStore", "0");
                para.SetValByKey("DataType", "1");
                para.SetValByKey("ExpDoc", "BP.App.Demo.BuessUnit_LocalWebApi_GenerToken");
                para.setMyPK(DBAccess.GenerGUID());
                para.Insert();
            }
            #endregion 数据源

            string enName = "TS.FrmUI.SFTableWebApiNoName";
            string enNameTree = "TS.FrmUI.SFTableWebApiTree";
            int idx = 0;

            BP.Sys.SFTable table = new BP.Sys.SFTable();

            #region 字典 - 无参数的字典.
            table.No = "Demo_SFTable_MyDeptEmps";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;

            if (table.RetrieveFromDBSources() == 0)
            {
                table.ResetDefaultVal();
                table.No = "Demo_SFTable_MyDeptEmps";

                table.setName("我部门的同事");
                table.SelectStatement = "/WF/API/Demo_SFTable_MyDeptEmps?token=@Token";
                table.SetValByKey("DBSrcType", "WebApi");
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", enName);
                table.SetValByKey("FK_SFDBSrc", "localWebApi");
                table.SetValByKey("FieldNo", "");
                table.SetValByKey("FieldName", "");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            table.No = "Demo_SFTable_MyDepts";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.ResetDefaultVal();
                table.No = "Demo_SFTable_MyDepts";

                table.setName("我的部门");
                table.SelectStatement = "/WF/API/Demo_SFTable_MyDepts?token=@Token";
                table.SFDBSrcNo = "local";
                table.SetPara("EnName", enName);
                table.SetValByKey("FK_SFDBSrc", "localWebApi");
                table.SetValByKey("DBSrcType", "WebApi");
                table.SetValByKey("RootNo", "@WebUser.DeptNo");
                table.CodeStruct = CodeStruct.NoName;
                table.SetValByKey("FieldNo", "");
                table.SetValByKey("FieldName", "");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            table.No = "Demo_SFTable_MyStations";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.ResetDefaultVal();
                table.No = "Demo_SFTable_MyStations";

                table.setName("我的岗位");
                table.SelectStatement = "/WF/API/Demo_SFTable_MyStations?token=@Token";
                table.SFDBSrcNo = "local";
                table.SetPara("EnName", enNameTree);
                table.SetValByKey("FK_SFDBSrc", "localWebApi");
                table.SetValByKey("DBSrcType", "WebApi");
                table.CodeStruct = CodeStruct.NoName;
                table.SetValByKey("FieldNo", "");
                table.SetValByKey("FieldName", "");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "Demo_SFTable_Depts";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.ResetDefaultVal();
                table.No = "Demo_SFTable_Depts";

                table.setName("部门");
                table.SelectStatement = "/WF/API/Demo_SFTable_Depts?token=@Token";
                table.SFDBSrcNo = "local";
                table.SetPara("EnName", enNameTree);
                table.SetValByKey("FK_SFDBSrc", "localWebApi");
                table.SetValByKey("DBSrcType", "WebApi");
                table.CodeStruct = CodeStruct.Tree;
                table.SetValByKey("RootVal", "100");
                table.SetValByKey("FieldNo", "");
                table.SetValByKey("FieldName", "");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "Demo_SFTable_ShengFen";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.ResetDefaultVal();
                table.No = "Demo_SFTable_ShengFen";

                table.setName("省份");
                table.SelectStatement = "/WF/API/Demo_SFTable_ShengFen?token=@Token";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", enName);
                table.SetValByKey("FK_SFDBSrc", "localWebApi");
                table.SetValByKey("DBSrcType", "WebApi");
                table.SetValByKey("FieldNo", "BianHao");
                table.SetValByKey("FieldName", "MingCheng");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            #endregion 字典 - 无参数的字典.

            #region 字典 - 有参数的字典.
            table.No = "Demo_SFTable_EmpsByDeptNo";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.ResetDefaultVal();
                table.No = "Demo_SFTable_EmpsByDeptNo";
                table.setName("根据部门编号获得人员");
                table.SelectStatement = "/WF/API/Demo_SFTable_EmpsByDeptNo?token=@Token&deptNo=@DeptNo";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", enName);
                table.SetValByKey("FK_SFDBSrc", "localWebApi");
                table.SetValByKey("DBSrcType", "WebApi");
                table.SetValByKey("ParamAlia", "DeptNo=FK_Dept");
                table.SetValByKey("IsPara", "1");
                table.SetValByKey("FieldNo", "");
                table.SetValByKey("FieldName", "");
                table.SetValByKey("TestParas", "@DeptNo=100");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }

            table.No = "Demo_SFTable_City";
            if (SystemConfig.CCBPMRunModel != CCBPMRunModel.Single && table.No.StartsWith(WebUser.OrgNo + "_") == false)
                table.No = WebUser.OrgNo + "_" + table.No;
            if (table.RetrieveFromDBSources() == 0)
            {
                table.ResetDefaultVal();
                table.No = "Demo_SFTable_City";

                table.setName("城市");
                table.SelectStatement = "/WF/API/Demo_SFTable_City?token=@Token&shengFenNo=@ShengFenNo";
                table.CodeStruct = CodeStruct.NoName;
                table.SetPara("EnName", enName);
                table.SetValByKey("FK_SFDBSrc", "localWebApi");
                table.SetValByKey("DBSrcType", "WebApi");
                table.SetValByKey("ParamAlia", "ShengFenNo=Key,SF,SFNo,");
                table.SetValByKey("IsPara", "1");
                table.SetValByKey("FieldNo", "BianHao");
                table.SetValByKey("FieldName", "MingCheng");
                table.SetValByKey("TestParas", "@ShengFenNo=53");
                idx = idx++;
                table.SetValByKey("Idx", idx);
                table.Insert();
            }
            #endregion 字典 - 无参数的字典.

            #region 查询(待办，在途，抄送. 草稿)
            SFSearch search = new SFSearch();
            search.SetValByKey("No", "Todolist");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("待办");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "/WF/API/DB_Todolist?token=@Token");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }

            //在途
            search.SetValByKey("No", "Runinglist");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("在途");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "/WF/API/DB_Runing?token=@Token");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }

            //抄送
            search.SetValByKey("No", "CClist");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("抄送");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "/WF/API/DB_CCList?token=@Token");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 查询.

            #region 过程(创建workID,发起,退回,移交,设置流程参数,删除,完成,发消息)
            SFProc proc = new SFProc();
            proc.SetValByKey("No", "Node_CreateBlankWorkID");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("创建workID");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Node_CreateBlankWorkID?token=@Token&flowNo=@FlowNo");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@FlowNo=001");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                proc.Insert();
            }
            //发送
            proc.SetValByKey("No", "Node_SendWork");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("发送");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Node_SendWork?token=@Token&workID=@WorkID&toNodeIDStr=@ToNodeIDStr&toEmps=@ToEmps&paras=@Paras&checkNote=@CheckNote");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@WorkID=@ToNodeIDStr=@ToEmps=admin@Paras=@CheckNote=审核通过");
                idx = idx++;
                proc.SetValByKey("Idx", idx);
                proc.Insert();
            }

            //退回
            proc.SetValByKey("No", "Node_ReturnWork");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("退回");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Node_ReturnWork?token=@Token&workID=@WorkID&returnToNodeIDStr=@ReturnToNodeIDStr&returnToEmp=@ReturnToEmp&msg=@Msg&isBackToThisNode=@IsBackToThisNode");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@WorkID=@ReturnToNodeIDStr=@ReturnToEmp=admin@Paras=@Msg=@IsBackToThisNode");
                idx = idx++;
                proc.SetValByKey("Idx", idx);
                proc.Insert();
            }
            //撤销
            proc.SetValByKey("No", "Flow_DoUnSend");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("撤销");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Flow_DoUnSend?token=@Token&workIDs=@WorkIDs");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@WorkIDs=");
                idx = idx++;
                proc.SetValByKey("Idx", idx);
                proc.Insert();
            }
            //移交
            proc.SetValByKey("No", "Node_Shift");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("移交");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Node_Shift?token=@Token&workID=@WorkID&toEmpNo=@ToEmpNo&msg=@Msg");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@WorkID=@ToEmpNo=yuwen@Msg=有事外出");
                idx = idx++;
                proc.SetValByKey("Idx", idx);
                proc.Insert();
            }
            //结束
            proc.SetValByKey("No", "Flow_DoFlowOver");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("结束");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Flow_DoFlowOver?token=@Token&workIDs=@WorkIDs&stopFlowType=@StopFlowType");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@WorkIDs=@StopFlowType=1");
                idx = idx++;
                proc.SetValByKey("Idx", idx);
                proc.Insert();
            }
            //删除
            proc.SetValByKey("No", "Batch_Delete");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("删除");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Batch_Delete?token=@Token&workIDs=@WorkIDs");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@WorkIDs=");
                idx = idx++;
                proc.SetValByKey("Idx", idx);
                proc.Insert();
            }
            //发送消息
            proc.SetValByKey("No", "Port_SendMessage");
            if (proc.RetrieveFromDBSources() == 0)
            {
                proc.setName("发送消息");
                proc.SetValByKey("FK_SFDBSrc", dbSrc.No);
                proc.SetValByKey("SelectStatement", "/WF/API/Port_SendMessage?token=@Token&toEmpNo=@ToEmpNo&title=@Title&message=@Message");
                proc.SetPara("EnName", "TS.FrmUI.SFProcWebApi");
                proc.SetValByKey("TestParas", "@ToEmpNo=admin@Title=消息发送测试@Message=消息测试xxxxxx");
                idx = idx++;
                proc.SetValByKey("Idx", idx);
                proc.Insert();
            }
            #endregion 过程.
        }
        /// <summary>
        /// 新建天气预报数据源
        /// </summary>
        public static void InitDBSrc_Weather()
        {
            #region 数据源
            SFDBSrc dbSrc = new SFDBSrc();
            dbSrc.No = "Weather";
            dbSrc.Name = "天气预报-和风API";
            if (dbSrc.RetrieveFromDBSources() == 0)
            {
                //dbSrc.DBSrcType = "WebApi";
                dbSrc.SetValByKey("DBSrcType", "WebApi"); //设置类型. 
                dbSrc.SetValByKey("ConnString", "https://devapi.qweather.com/v7/weather/");
                dbSrc.SetPara("EnName", "TS.Sys.SFDBSrcWebApi");
                dbSrc.SetValByKey("TokenGetWay", "None");
                dbSrc.SetValByKey("WebApiResultModel", "1");
                dbSrc.SetValByKey("WebApiResultObjEnName", "BP.App.Demo.Weather.BuessUnit_ResultObj_WeatherWebApi");
                dbSrc.SetValByKey("WebApiResultObjEnNameT", "ResultObj:天气预报数据结果转换");
                dbSrc.Insert();
            }
            //全局参数.
            SFApiParas paras = new SFApiParas();
            paras.Retrieve("AttrKey", "Key", "DBSrcNo", dbSrc.No);
            if (paras.Count == 0)
            {
                SFApiPara para = new SFApiPara();
                para.SetValByKey("AttrKey", "Key");
                para.SetValByKey("DBSrcNo", dbSrc.No);
                para.SetValByKey("AttrName", "(天气预报API)获取Key");
                para.SetValByKey("ApiParaModel", "1");
                para.SetValByKey("ApiParaStore", "0");
                para.SetValByKey("DataType", "1");
                para.SetValByKey("ExpDoc", "BP.App.Demo.Weather.BuessUnit_WeatherWebApi_GenerKey");
                para.setMyPK(DBAccess.GenerGUID());
                para.Insert();
            }
            #endregion 数据源

            #region 无参数.
            SFSearch search = new SFSearch();
            int idx = 0;
            search.SetValByKey("No", "BJCityWeather");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("北京天气预报");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "3d?location=101010100&key=@Key");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }

            search.SetValByKey("No", "JNCityWeather");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("济南天气预报");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "3d?location=101120101&key=@Key");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 无参数.

            #region 有参数.
            search = new SFSearch();
            search.SetValByKey("No", "SpecCity");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("指定城市");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "3d?location=@location&key=@Key");
                search.SetValByKey("TestParas", "@location=101120101");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 有参数.
        }
        /// <summary>
        /// 新建股票数据数据源
        /// </summary>
        public static void InitDBSrc_Stock()
        {
            #region 数据源
            SFDBSrc dbSrc = new SFDBSrc();
            dbSrc.No = "Stock";
            dbSrc.Name = "股票-东方API(当天)";
            if (dbSrc.RetrieveFromDBSources() == 0)
            {
                //dbSrc.DBSrcType = "WebApi";
                dbSrc.SetValByKey("DBSrcType", "WebApi"); //设置类型. 
                dbSrc.SetValByKey("ConnString", "http://push2.eastmoney.com/api/qt/stock/get");
                dbSrc.SetPara("EnName", "TS.Sys.SFDBSrcWebApi");
                dbSrc.SetValByKey("TokenGetWay", "None");
                dbSrc.SetValByKey("WebApiResultModel", "1");
                dbSrc.SetValByKey("WebApiResultObjEnName", "BP.App.Demo.Stock.BuessUnit_ResultObj_TradeWebApi");
                dbSrc.SetValByKey("WebApiResultObjEnNameT", "ResultObj:股票数据结果转换");
                dbSrc.Insert();
            }
           
            #endregion 数据源

            #region 无参数.
            SFSearch search = new SFSearch();
            int idx = 0;
            search.SetValByKey("No", "MoutaiStockCode");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("贵州茅台");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "?secid=1.600519");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }

            search.SetValByKey("No", "TechnologyStockCode");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("科创50");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "?secid=1.000688");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 无参数.

            #region 有参数.
            search = new SFSearch();
            search.SetValByKey("No", "StockCode");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("指定股票代码-当天数据");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "?secid=@location");
                search.SetValByKey("TestParas", "@location=1.600519");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 有参数.


            #region 历史API.

            dbSrc = new SFDBSrc();
            dbSrc.No = "StockHistory";
            dbSrc.Name = "股票-东方API(历史)";
            if (dbSrc.RetrieveFromDBSources() == 0)
            {
                //dbSrc.DBSrcType = "WebApi";
                dbSrc.SetValByKey("DBSrcType", "WebApi"); //设置类型. 
                dbSrc.SetValByKey("ConnString", "http://push2his.eastmoney.com/api/qt/stock/");
                dbSrc.SetPara("EnName", "TS.Sys.SFDBSrcWebApi");
                dbSrc.SetValByKey("TokenGetWay", "None");
                dbSrc.SetValByKey("WebApiResultModel", "1");
                dbSrc.SetValByKey("WebApiResultObjEnName", "BP.App.Demo.Stock.BuessUnit_ResultObj_TradeYearWebApi");
                dbSrc.SetValByKey("WebApiResultObjEnNameT", "ResultObj:股票数据结果转换");
                dbSrc.Insert();
            }
         

            search = new SFSearch();
            search.SetValByKey("No", "StockHistory");
            if (search.RetrieveFromDBSources() == 0)
            {

                search.setName("指定股票代码");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "kline/get?" + "secid=@secid&klt=101&fqt=1&beg=@begDate&end=@endDate&fields1=f1,f2,f3,f4,f5&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61");
                
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 历史API.

            #region 批量获取多支股票API.批量实时行情接口：单次上限（建议不超过50只股票）
            dbSrc = new SFDBSrc();
            dbSrc.No = "MultipleStocks";
            dbSrc.Name = "股票-东方API(集合)";
            if (dbSrc.RetrieveFromDBSources() == 0)
            {
                dbSrc.SetValByKey("DBSrcType", "WebApi"); //设置类型. 
                dbSrc.SetValByKey("ConnString", "http://push2.eastmoney.com/api/qt/");
                dbSrc.SetPara("EnName", "TS.Sys.SFDBSrcWebApi@WebApiResultObjEnNameT=ResultObj: 批量获取股票集合-数据结果转换.");
                dbSrc.SetValByKey("TokenGetWay", "None");
                dbSrc.SetValByKey("WebApiResultModel", "1");
                dbSrc.SetValByKey("WebApiResultObjEnName", "BP.App.Demo.Stock.BuessUnit_ResultObj_TradeBatchWebApi");
                dbSrc.SetValByKey("WebApiResultObjEnNameT", "ResultObj:股票集合数据结果转换");
                dbSrc.Insert();
            }


            search = new SFSearch();
            search.SetValByKey("No", "MultipleStocks");
            if (search.RetrieveFromDBSources() == 0)
            {
                search.setName("多支股票集合");
                search.SetValByKey("FK_SFDBSrc", dbSrc.No);
                search.SetValByKey("SelectStatement", "ulist.np/get?" + "fields=f2,f3,f4,f12,f13,f14,f20,f21,f62&secids=@secids");
                search.SetValByKey("TestParas", "@secids=1.600519,0.000858,0.000568,1.600809,1.603369");
                search.SetPara("EnName", "TS.FrmUI.SFSearchWebApi");
                idx = idx++;
                search.SetValByKey("Idx", idx);
                search.Insert();
            }
            #endregion 批量获取多支股票API.

        }
        /// <summary>
        /// 如果发现升级sql文件日期变化了，就自动升级.
        /// 就是说该文件如果被修改了就会自动升级.
        /// </summary>
        public static void UpdataCCFlowVerSQLScript()
        {

            string sql = "SELECT IntVal FROM Sys_Serial WHERE CfgKey='UpdataCCFlowVer'";
            string currDBVer = DBAccess.RunSQLReturnStringIsNull(sql, "");

            string sqlScript = BP.Difference.SystemConfig.PathOfData + "UpdataCCFlowVer.sql";
            System.IO.FileInfo fi = new System.IO.FileInfo(sqlScript);
            if (File.Exists(sqlScript) == false)
                return;
            string myVer = fi.LastWriteTime.ToString("yyyyMMddHH");

            //判断是否可以执行，当文件发生变化后，才执行。
            if (currDBVer == "" || int.Parse(currDBVer) < int.Parse(myVer))
            {
                DBAccess.RunSQLScript(sqlScript);
                sql = "UPDATE Sys_Serial SET IntVal=" + myVer + " WHERE CfgKey='UpdataCCFlowVer'";

                if (DBAccess.RunSQL(sql) == 0)
                {
                    sql = "INSERT INTO Sys_Serial (CfgKey,IntVal) VALUES('UpdataCCFlowVer'," + myVer + ") ";
                    DBAccess.RunSQL(sql);
                }
            }
        }

        public static void UpdateMapExtFullData(MapExt item, string refPKVal)
        {
            //分析Tag1 ； 填充从表。
            if (DataType.IsNullOrEmpty(item.Tag1) == false && item.Tag1.Contains(":"))
            {
                string[] strs = item.Tag1.Split("$");
                foreach (string str in strs)
                {
                    if (DataType.IsNullOrEmpty(str) == true)
                        continue;
                    string[] ss = str.Split(':');
                    if (ss.Length == 2)
                    {
                        string dtlName = ss[0];
                        string dtlSql = ss[1];
                        MapExt me = new MapExt();
                        if (item.ExtModel.Equals("PageLoadFullMainTable"))
                        {
                            me.MyPK = dtlName + "_PageLoadFullDtl";
                            me.ExtType = "PageLoadFullDtl";
                            me.ExtModel = "PageLoadFullDtl";
                            me.setFK_MapData(dtlName);
                        }
                        else
                        {
                            me.MyPK = item.FrmID + "_" + item.AttrOfOper + "_" + dtlName;
                            me.ExtType = "FullDataDtl";
                            me.ExtModel = "FullDataDtl";
                            me.setFK_MapData(item.FrmID);
                            me.AttrOfOper = item.AttrOfOper;
                            me.SetPara("EnName", "TS.MapExt.FullDtlSelf");
                        }
                        me.DoWay = "Self";
                        me.Doc = dtlSql;
                        me.Tag1 = dtlName;
                        me.RefPKVal = refPKVal;
                        me.DirectInsert();
                    }
                }
            }
            //填充下拉框
            if (DataType.IsNullOrEmpty(item.Tag) == false && item.Tag.Contains(":"))
            {
                string[] strs = item.Tag.Split("$");
                foreach (string str in strs)
                {
                    if (DataType.IsNullOrEmpty(str) == true)
                        continue;
                    string[] ss = str.Split(':');
                    if (ss.Length == 2)
                    {
                        string keyOfEn = ss[0];
                        string sql = ss[1];
                        MapExt me = new MapExt();
                        me.MyPK = refPKVal + "_" + keyOfEn;
                        me.ExtType = "FullDataDDL";
                        me.ExtModel = "FullDataDDL";
                        me.DoWay = "Self";
                        me.Doc = sql;
                        me.Tag1 = keyOfEn;
                        me.AttrOfOper = item.AttrOfOper;
                        me.setFK_MapData(item.FrmID);
                        me.SetPara("EnName", "TS.MapExt.FullDDLSelf");
                        me.RefPKVal = refPKVal;
                        me.Insert();
                    }
                }
            }
        }
        public static string UpdateMapExtH5()
        {

            #region 升级数据源 2024.10.12.
            SFProc pro = new SFProc();
            pro.CheckPhysicsTable();

            if (DBAccess.IsExitsTableCol("Sys_SFTable", "SrcType"))
            {
                //升级数据源.
                DBAccess.RunSQL("UPDATE  Sys_SFTable SET DBSrcType='SQL' WHERE SrcType=3 AND (DBSrcType IS NULL OR DBSrcType='')");
                DBAccess.DropTableColumn("Sys_SFTable", "SrcType");
            }

            FrmEvent f1e = new FrmEvent();
            f1e.CheckPhysicsTable();
            if (DBAccess.IsExitsTableCol("Sys_FrmEvent", "FK_MapData"))
            {
                DBAccess.RunSQL("UPDATE Sys_FrmEvent SET FrmID=FK_MapData");
                DBAccess.DropTableColumn("Sys_FrmEvent", "FK_MapData");
            }
            if (DBAccess.IsExitsTableCol("Sys_FrmEvent", "FK_Event"))
            {
                DBAccess.RunSQL("UPDATE Sys_FrmEvent SET EventID=FK_Event");
                DBAccess.DropTableColumn("Sys_FrmEvent", "FK_Event");
            }
            DBAccess.RunSQL("UPDATE Sys_FrmEvent set atpara='@EnName=TS.Sys.SysEventDBSrc' WHERE AtPara Is Null Or atPara=''");

            //表单事件
            DBAccess.RunSQL("UPDATE Sys_FrmEvent SET RefPKVal=FrmID WHERE (RefPKVal Is Null Or RefPKVal='') AND EventSource=0");

            //节点事件
            DBAccess.RunSQL("UPDATE Sys_FrmEvent SET RefPKVal=FK_Node WHERE (RefPKVal Is Null Or RefPKVal='') AND EventSource=2");

            //流程事件
            DBAccess.RunSQL("UPDATE Sys_FrmEvent SET RefPKVal=RefFlowNo WHERE (RefPKVal Is Null Or RefPKVal='') AND EventSource=1");

            //查询出来没有处理的数据.
            string mysql23 = "SELECT MyPK FROM Sys_FrmEvent WHERE EventDoTypeT is null or EventDoTypeT=''";
            FrmEvents fees = new FrmEvents();
            fees.RetrieveIn("MyPK", mysql23);
            foreach (FrmEvent item in fees)
            {
                if (item.EventDoType.Equals("1"))
                {
                    item.EventDoType = "2";
                    item.SetValByKey("EventDoTypeT", "数据源模式");
                    item.Update(); //更新.
                }
                //if (DataType.IsNullOrEmpty( item.EventName) ==true)
                //{
                //    item.EventDoType = "2";
                //    item.SetValByKey("EventDoTypeT", "数据源模式");
                //    item.Update(); //更新.
                //}
            }
            DBAccess.RunSQL("UPDATE Sys_FrmEvent SET EventName=EventID WHERE EventName='' OR EventName IS NULL");
            #endregion 升级数据源 2024.10.12.

            #region 升级按钮. OK.
            string sqlBtn = "SELECT MyPK FROM Sys_MapAttr WHERE UIContralType=18";
            MapAttrs attrsBtn = new MapAttrs();
            attrsBtn.RetrieveIn("MyPK", sqlBtn);

            foreach (MapAttr item in attrsBtn)
            {
                item.SetPara("EnName", "TS.FrmUI.SelfCommonent.FrmBtn");
                item.Update();

                //检查是否激活了填充?
                MapExt ext = new MapExt();
                ext.MyPK = item.MyPK + "_FullData";
                if (ext.RetrieveFromDBSources() == 0)
                    continue;
                ext.Delete();

                ext.MyPK = item.MyPK + "_DDLFullCtrl";
                ext.DoWay = "1";
                ext.Tag5 = "Self";
                ext.ExtModel = "DDLFullCtrl";
                ext.ExtType = "DDLFullCtrl";
                ext.Tag6 = ext.Doc;
                ext.ItisLoadFull = true;
                ext.Insert();

                UpdateMapExtFullData(ext, ext.MyPK);
            }
            #endregion 升级按钮. OK.

            #region 升级Pop弹窗
            string popStr = "'PopBranchesAndLeaf','PopBranches','PopGroupList','PopTableList','PopBindSFTable','PopBindEnum','PopTableSearch','PopSelfUrl'";
            MapExts exts = new MapExts();
            exts.RetrieveIn(MapExtAttr.ExtType, popStr);

            //获取弹窗的MapAttr
            MapAttrs attrs = new MapAttrs();
            attrs.RetrieveByLike("AtPara", "'%@PopModel=%'");

            string mysql1 = "SELECT MyPK FROM Sys_MapExt WHERE ExtType='FullData' AND (DoWay='1' OR (MyPK Like 'TBFullCtrl_%' AND DoWay='0'))";
            MapExts ext1s = new MapExts();
            ext1s.RetrieveInSQL(mysql1);
            string extMyPKs = ",";
            MapAttr attr = null;
            foreach (MapExt ext in exts)
            {
                attr = attrs.GetEntityByKey(ext.FrmID + "_" + ext.AttrOfOper) as MapAttr;
                if (attr == null)
                    continue;
                string popModel = attr.GetParaString("PopModel", "");
                if (popModel.Equals(ext.ExtType) == false)
                    continue;

                ext.DoWay = ext.ExtType;
                if (ext.ExtType.Equals("PopTableSearch"))
                    ext.DoWay = "PopTable";
                string mypk = ext.FrmID + "_" + ext.AttrOfOper + "_Pop";
                DBAccess.RunSQL("UPDATE Sys_MapExt SET DoWay='" + ext.DoWay + "',ExtModel='Pop',ExtType='Pop',MyPK='" + mypk + "' Where MyPK='" + ext.MyPK + "'");
                MapExt newExt = new MapExt(mypk);
                Entity en = ext1s.GetEntityByKey(ext.MyPK + "_FullData");
                if (en == null)
                    continue;
                MapExt fullDataExt = en as MapExt;
                fullDataExt.Delete();
                extMyPKs += fullDataExt.MyPK + ",";
                newExt.Tag6 = fullDataExt.Doc;
                newExt.Tag5 = "Self";
                newExt.ItisLoadFull = true;
                newExt.Update();
                UpdateMapExtFullData(fullDataExt, newExt.MyPK);
            }
            #endregion 升级Pop弹窗

            foreach (MapExt item in ext1s)
            {
                if (extMyPKs.Contains("," + item.MyPK + ","))
                    continue;
                item.Delete(); //删除数据.
                if (item.MyPK.StartsWith("TBFullCtrl_"))
                {
                    attr = new MapAttr(item.FrmID + "_" + item.AttrOfOper);
                    string type = attr.atPara.GetValStrByKey("TBFullCtrl");
                    if (type.Equals("Table"))
                        item.DoWay = "Table";
                    else if (type.Equals("Simple"))
                        item.DoWay = "Simple";
                    else
                        item.DoWay = "None";
                    if (item.DoWay.Equals("None"))
                        continue;
                    item.ExtType = "TBFullCtrl";
                    item.ExtModel = "TBFullCtrl";
                    item.Tag5 = "Self";
                    item.Tag6 = item.Doc;
                    item.RefPKVal = item.FrmID + "_" + item.AttrOfOper;
                    item.MyPK = item.FrmID + "_" + item.AttrOfOper + "_TBFullCtrl";
                    if (item.Update() == 0)
                        item.Insert();
                }
                else
                {
                    item.DoWay = "Self";
                    item.ExtModel = "FullData";
                    item.MyPK = item.FrmID + "_" + item.AttrOfOper + "_" + item.ExtModel;
                    item.SetValByKey("ExtType", "FullData");

                    item.Tag6 = item.Doc;
                    item.Tag5 = "Self";
                    item.RefPKVal = item.FrmID + "_" + item.AttrOfOper;
                    item.ItisLoadFull = true;
                    if (item.Update() == 0)
                        item.Insert();
                }
                UpdateMapExtFullData(item, item.MyPK);
            }

            #region 升级装载填充.
            mysql1 = "select MyPK from Sys_MapExt where ExtType='PageLoadFull'";
            exts = new MapExts();
            exts.RetrieveInSQL(mysql1);
            foreach (MapExt item in exts)
            {

                item.Delete(); //删除数据.

                item.DoWay = "Self";
                item.ExtModel = "PageLoadFullMainTable";
                item.MyPK = item.FrmID + "_" + item.ExtModel;
                item.SetValByKey("ExtType", "PageLoadFullMainTable");

                item.AttrOfOper = "";

                item.Doc = item.Doc;
                //item.RefPKVal = item.FrmID + "_" + item.AttrOfOper;
                item.ItisLoadFull = true;
                if (item.DirectUpdate() == 0)
                    item.DirectInsert();

                UpdateMapExtFullData(item, item.MyPK);
            }
            #endregion 升级装载填充.

            #region 升级 ActiveDDL 
            //查询有问题.
            mysql1 = "Select MyPK from Sys_MapExt WHERE MyPK like 'ActiveDDL_%'  ";
            exts = new MapExts();
            exts.RetrieveInSQL(mysql1);
            foreach (MapExt item in exts)
            {
                item.Delete(); //删除数据.
                item.DoWay = "1";
                item.ExtModel = "ActiveDDL";
                item.SetValByKey("ExtType", "ActiveDDL");
                item.SetPara("IsSelectVal", 1);
                string mypk = item.FrmID + "_" + item.AttrOfOper + "_" + item.ExtModel;
                item.setMyPK(mypk);
                // DBAccess.RunSQL("UPDATE Sys_MapExt SET DoWay='1',ExtModel='ActiveDDL',ExtType='ActiveDDL',MyPK='" + mypk + "' Where MyPK='" + item.MyPK + "'");
                if (item.DirectUpdate() == 0)
                    item.DirectInsert();
            }
            #endregion 升级 ActiveDDL 

            #region 自动计算
            exts = new MapExts();
            exts.Retrieve(MapExtAttr.ExtType, MapExtXmlList.AutoFull);
            foreach (MapExt ext in exts)
            {
                if (DataType.IsNullOrEmpty(ext.Doc) == true)
                    continue;
                ext.Delete();
                ext.DoWay = "1";
                ext.ExtModel = MapExtXmlList.AutoFull;
                ext.Tag = ext.Doc;
                string mypk = ext.FrmID + "_" + ext.AttrOfOper + "_" + ext.ExtModel;
                //DBAccess.RunSQL("UPDATE Sys_MapExt SET DoWay='1',ExtModel='AutoFull',ExtType='AutoFull',Tag=Doc,MyPK='" + mypk + "' Where MyPK='" + ext.MyPK + "'");
                ext.setMyPK(mypk);
                if (ext.Update() == 0)
                    ext.Insert();
            }
            #endregion 自动计算

            #region 升级正则表达式
            exts = new MapExts();
            exts.Retrieve(MapExtAttr.ExtType, MapExtXmlList.RegularExpression);
            foreach (MapExt ext in exts)
            {
                if (DataType.IsNullOrEmpty(ext.Doc) == true)
                    continue;
                ext.Delete();
                if (ext.Tag.StartsWith("on") == false)
                    continue;
                ext.ExtModel = "BindFunction";
                ext.ExtType = MapExtXmlList.RegularExpression;
                ext.RefPKVal = ext.FrmID + "_" + ext.AttrOfOper;
                ext.Tag = ext.Tag.Substring(2);
                ext.Tag1 = ext.Tag + "执行事件";
                ext.Tag2 = ext.Tag1;
                ext.MyPK = DBAccess.GenerGUID();
                ext.SetPara("EnName", "TS.MapExt.RegularExpression");

                if (ext.Update() == 0)
                    ext.Insert();
            }
            #endregion 升级正则表达式

            #region 升级小范围、多范围
            exts = new MapExts();
            exts.RetrieveIn(MapExtAttr.ExtType, "'MultipleChoiceSmall', 'SingleChoiceSmall'");
            foreach (MapExt ext in exts)
            {
                ext.ExtModel = ext.ExtType;
                string mypk = ext.FrmID + "_" + ext.AttrOfOper + "_" + ext.ExtType;
                DBAccess.RunSQL("UPDATE Sys_MapExt SET ExtModel=ExtType,MyPK='" + mypk + "' Where MyPK='" + ext.MyPK + "'");
            }
            #endregion 升级小范围、多范围

            #region 时间限制规则
            exts = new MapExts();
            exts.Retrieve(MapExtAttr.ExtType, "DataFieldInputRole");
            foreach (MapExt ext in exts)
            {
                string mypk = ext.FrmID + "_" + ext.AttrOfOper + "_" + ext.ExtType;
                ext.ExtModel = ext.ExtType;
                if (ext.Tag2.Equals("0"))
                {
                    ext.DoWay = "1";
                    DBAccess.RunSQL("UPDATE Sys_MapExt SET DoWay='1',ExtModel='DateFieldInputRole',ExtType='DateFieldInputRole',MyPK='" + mypk + "' Where MyPK='" + ext.MyPK + "'");
                    continue;

                }
                ext.DoWay = "2";

                if (ext.Tag2.Equals("1"))
                {
                    ext.Tag1 = ext.Tag4;
                    string tag3 = ext.Tag3; //@GT=大于@GTE=大于等于@IT=小于@ITE=小于等于@EQ=等于@NEQ=不等于
                    switch (tag3)
                    {
                        case "dayu":
                            ext.Tag = "GT";
                            break;
                        case "dayudengyu":
                            ext.Tag = "GTE";
                            break;
                        case "xiaoyu":
                            ext.Tag = "IT";
                            break;
                        case "xiaoyudengyu":
                            ext.Tag = "ITE";
                            break;
                        case "dengyu":
                            ext.Tag = "EQ";
                            break;
                        case "budengyu":
                            ext.Tag = "NEQ";
                            break;
                        default: break;

                    }
                }
                DBAccess.RunSQL("UPDATE Sys_MapExt SET DoWay='2',ExtModel='DateFieldInputRole',ExtType='DateFieldInputRole',Tag='" + ext.Tag + "',Tag1=Tag4 ,MyPK='" + mypk + "' Where MyPK='" + ext.MyPK + "'");
            }
            #endregion 时间限制规则

            return "升级成功.";
        }
    }

}
